From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Script checking to see what database it's connected to
Date: Sun, 21 Feb 2021 18:42:50 -0600
Message-ID: <e3b70f2b-3ab9-a855-6ed9-68d0868e6eac@gmail.com> (raw)
In-Reply-To: <747b62a4-128c-093e-e212-252c34cea42c@gmail.com>
References: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com>
<747b62a4-128c-093e-e212-252c34cea42c@gmail.com>
On 2/21/21 5:26 PM, Rob Sargent wrote:
>
>
> On 2/21/21 4:18 PM, Ron wrote:
>>
>> Postgresql 12.5
>>
>> I've got scripts which can run on multiple database (dev, test, QA,
>> Integration, Training, etc, etc), so of course I've got to run them like
>> "psql my_db_name -f script.sql".
>>
>> Of course, I sometimes forget to specify the database name, and so it fails.
>>
>> 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.
>>
>> This is what I've tried so far:
>>
>> postgres=# \if :DBNAME == postgres
>> unrecognized value "postgres == postgres" for "\if expression": Boolean
>> expected
>> postgres@#
>>
>> postgres=# \if ':DBNAME' == postgres
>> unrecognized value ":DBNAME == postgres" for "\if expression": Boolean
>> expected
>> postgres@#
>>
>> postgres=# \if :DBNAME == 'postgres'
>> unrecognized value "postgres == postgres" for "\if expression": Boolean
>> expected
>> postgres@#
>>
>> postgres=# \if ':DBNAME' == 'postgres'
>> unrecognized value ":DBNAME == postgres" for "\if expression": Boolean
>> expected
>> postgres@#
>>
>>
>
> Take it up a notch? Write a script which takes the dbname and the script
> name:
>
> /pcode/
>
> #!/bin/bash -e
> if [[ $# -ne 2 ]]; then echo "missing arg(s)"; exit 2; fi
> dbn=$1; shift;
> sql=$1; shift;
> psql --dbname $dbn --file $sql
>
> /pcode/
I thought of that, yet so earnestly want avoid Yet Another Tiny Script.
--
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: <e3b70f2b-3ab9-a855-6ed9-68d0868e6eac@gmail.com>
References: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com> <747b62a4-128c-093e-e212-252c34cea42c@gmail.com> <e3b70f2b-3ab9-a855-6ed9-68d0868e6eac@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