Create schema with in a specific database from a script file

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Abhra Kar <abhra(dot)kar(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Create schema with in a specific database from a script file
Date: 2018-02-02 02:56:45
Message-ID: CAKFQuwYvTudTkEuhqxDZJX9Qugb6SDqxn5F+pa7A3iTr-cj3qA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thursday, February 1, 2018, Abhra Kar <abhra(dot)kar(at)gmail(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sargez 2018-02-02 07:30:40 Re: Master-Slave error: the database system is starting up
Previous Message Melvin Davidson 2018-02-02 02:53:16 Re: Create schema with in a specific database from a script file