Re: Script checking to see what database it's connected to

From: Julien Rouhaud <rjuju123(at)gmail(dot)com>
To: Ron <ronljohnsonjr(at)gmail(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Script checking to see what database it's connected to
Date: 2021-02-22 01:19:37
Message-ID: CAOBaU_Yq3vi_RuUgrPYgpruVxxBLqOAfhTCG0x+0knZ-Q6_Gwg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Feb 22, 2021 at 9:00 AM Ron <ronljohnsonjr(at)gmail(dot)com> wrote:
>
> On 2/21/21 5:26 PM, Julien Rouhaud wrote:
>
> On Mon, Feb 22, 2021 at 7:19 AM Ron <ronljohnsonjr(at)gmail(dot)com> wrote:
>
> Thus, I want to add a bit to the top of the script, something like this:
>
> \if :DBNAME = postgres
> echo "must not run in postgres"
> exit
> \endif
>
> However, I can't seem to find the magic sauce.
>
> You have to use a dedicated variable. Something like
>
> SELECT :'DBNAME' = 'postgres' AS is_postgres \gset
>
>
> That works...
>
> \if :is_postgres
> [...]
>
>
> That almost works,
>
> My script:
> $ cat test_pg.sql
> SELECT :'DBNAME' = 'postgres' AS is_postgres \gset
> \echo :is_postgres
> \if :is_postgres
> \echo 'connected to postgres'
> exit
> \else
> \echo 'not connected to postgres'
> \endif
>
> The output:
> $ psql12 -f test_pg.sql
> t
> connected to postgres
> got here
> psql:test_pg.sql:15: ERROR: syntax error at or near "exit"
> LINE 1: exit

Well, the supported commands did work. You should probably look at
https://www.postgresql.org/docs/current/app-psql.html, you'd see that
"exit" is not a supported command and you should instead use \q[uit].
I recommend looking at the semantics of \quit though, given your next
message mentioning \include.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jagmohan Kaintura 2021-02-22 02:14:08 PostgreSQL Statement Dependency Resolving | Schema Upgrade
Previous Message Ron 2021-02-22 01:03:41 Re: Script checking to see what database it's connected to