Create schema with in a specific database from a script file - Mailing list pgsql-general

From David G. Johnston
Subject Create schema with in a specific database from a script file
Date
Msg-id CAKFQuwYvTudTkEuhqxDZJX9Qugb6SDqxn5F+pa7A3iTr-cj3qA@mail.gmail.com
Whole thread Raw
In response to Create schema with in a specific database from a script file  (Abhra Kar <abhra.kar@gmail.com>)
List pgsql-general
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.

pgsql-general by date:

Previous
From: Melvin Davidson
Date:
Subject: Re: Create schema with in a specific database from a script file
Next
From: Sargez
Date:
Subject: Re: Master-Slave error: the database system is starting up