Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDzaM-0005P6-Bi for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 01:00:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lDzaL-0000ii-9d for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 01:00:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDzaL-0000ia-0G for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 01:00:25 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lDzaI-0004Ph-NA for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 01:00:24 +0000 Received: by mail-ot1-x330.google.com with SMTP id b16so10575138otq.1 for ; Sun, 21 Feb 2021 17:00:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=67UzXdTlK3mzpCgU6gUbzK7uY4pNU05VjZ9F3H1uiX0=; b=gz4gz2Hl3Y95d4xPHUhUCwAV6Qf0OTmVDsWYiT1Jpw7ZxxENeKJgG0Xy1ygX54ptbp KBN4sk64UC1OWChm+jB2xv91uoTt0NCUbJuFO3iAbkenIUApT6FdE/RYFgWoH+YhSxLw iIWvODSKQ2xHMThoVgA4wJncPNbMuevokT8iNq4aaMjDqpznOjks5tGfHX6BSjTqJjaK 3iclmZ+ZFf/JQpf08WfOdNFQH/lgFwDb/96UBgS5BW2Zmvs3UvM9PQvcny8IkDDqNsHC QjYe7FVT0xI47MXzKwWkivAZ78kni8wdU6OxrfI6WOs8B3d/XQG0dg5bpDN/0iXT2ys2 RZww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=67UzXdTlK3mzpCgU6gUbzK7uY4pNU05VjZ9F3H1uiX0=; b=WX+r3ko1Bec7N3rNZ0naHNoYhTdqIAvs2/FKyHBQlq7ifczlyPqY8XZmqYm4IoXht0 JyBNB/pz8CnUugw7gQIaaEkhD916mDx1GK+lCHuM7dBzbjdywL0I7XxEl21ewgRE8ccm QYAUy8L8gipgBLh2WYBLglnYJ1aK1yoBx7rvjXo/vkb4NgCpif8jYN3SjJRVOvHt0hdr fO3Pp8aQ3RUYx9dXIUUZ5p0X5b+0vqKQKU68qG9I71J6GLeyJzdqpo+u3yPalO2Aloze fkCje0UWMotq30kmoVwjWsEvaSuBcKnc5SXa3LTNxI3x81AxP9O+AoiZpt0r6dgMKO6z fovQ== X-Gm-Message-State: AOAM530MlLv6oAr9RJhgZYIuW5ct/qRdba0rXKreWEOIFnnoRrlHB9F6 MBYKOalSCRACSg1bexiZ8hed66bgTBiMsw== X-Google-Smtp-Source: ABdhPJy8Qn+/MmI/j3HOaekNuvEddJhl5sKUjlaALd6+LGRHuAR0AicS7+0BRfC75ELxEv7GcgUPFg== X-Received: by 2002:a9d:7a88:: with SMTP id l8mr2322267otn.289.1613955621337; Sun, 21 Feb 2021 17:00:21 -0800 (PST) Received: from [192.168.88.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id n11sm449555oij.51.2021.02.21.17.00.20 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 21 Feb 2021 17:00:20 -0800 (PST) Subject: Re: Script checking to see what database it's connected to To: "pgsql-generallists.postgresql.org" References: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com> From: Ron Message-ID: <222764a0-5650-5457-e536-488861cdd666@gmail.com> Date: Sun, 21 Feb 2021 19:00:20 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------41FEB097B0B080802B2D89F9" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------41FEB097B0B080802B2D89F9 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2/21/21 5:26 PM, Julien Rouhaud wrote: > On Mon, Feb 22, 2021 at 7:19 AM Ron 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. --------------41FEB097B0B080802B2D89F9 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit 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.
--------------41FEB097B0B080802B2D89F9--