From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Script checking to see what database it's connected to
Date: Sun, 21 Feb 2021 19:00:20 -0600
Message-ID: <222764a0-5650-5457-e536-488861cdd666@gmail.com> (raw)
In-Reply-To: <CAOBaU_ZeHj6FWJQiOMOKoxoe7DXLshoaCoLL0w1b3hpg++VJRQ@mail.gmail.com>
References: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com>
<CAOBaU_ZeHj6FWJQiOMOKoxoe7DXLshoaCoLL0w1b3hpg++VJRQ@mail.gmail.com>
On 2/21/21 5:26 PM, Julien Rouhaud wrote:
> On Mon, Feb 22, 2021 at 7:19 AM Ron <ronljohnsonjr@gmail.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
--
Angular momentum makes the world go 'round.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: ronljohnsonjr@gmail.com
Subject: Re: Script checking to see what database it's connected to
In-Reply-To: <222764a0-5650-5457-e536-488861cdd666@gmail.com>
References: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com> <CAOBaU_ZeHj6FWJQiOMOKoxoe7DXLshoaCoLL0w1b3hpg++VJRQ@mail.gmail.com> <222764a0-5650-5457-e536-488861cdd666@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox