On Thursday, February 1, 2018, Abhra Kar <
abhra.kar@gmail.com> wrote:
In xyz.sh I executed the following script ---
su -c "psql -c \"\c ABC \"" postgres
su -c "psql -c \"create schema authorization myschema\"" postgres
In the terminal got message “connected to ABC database”. But schema created with in postgres database not with in ABC database.
Right. psql connected to the Postgres database, executed \c ABC which caused the session to connect to ABC. Then, seeing no other commands, that psql session disconnected. Then a brand new session was opened to the postgres database and in that new session created the schema.
Every psql command in a script is independent of all others, there is no state maintained.
For psql scripts in bash I find:
psql <<SQL
<script here>
SQL
To be the better form than trying to use -c. Or put the psql code in its own file and use -f (really preferred though more files and navigation to deal with).
And you should probably try to avoid using "su" as a crutch to make things work. Invoking commands as root is dangerous.
David J.