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 1lDy7v-0002SQ-9d for pgsql-general@arkaria.postgresql.org; Sun, 21 Feb 2021 23:26:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lDy7u-0007KX-7X for pgsql-general@arkaria.postgresql.org; Sun, 21 Feb 2021 23:26:58 +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 1lDy7t-0007KP-WA for pgsql-general@lists.postgresql.org; Sun, 21 Feb 2021 23:26:57 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lDy7s-0003kE-1T for pgsql-general@lists.postgresql.org; Sun, 21 Feb 2021 23:26:56 +0000 Received: by mail-oi1-x236.google.com with SMTP id l133so3987014oib.4 for ; Sun, 21 Feb 2021 15:26:55 -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:content-transfer-encoding; bh=p8rqDRWOnwKnCpFlv14KT1UvUbw0sJGmcvPKkVafyTg=; b=BOSru/YQUa0IBnixbD0V/vkRXuf+wc9MHh19u5/XACZ7u5tKBJDmVSy+Ta0sTNU5FK qUmI3Jz/jeoQRaQuaH/NM+I0jVmadSBQYuV6ZBIKcEEccTYZ7HA1YFJekC1zejhUXwr1 MZC4Y84AKNHjf4sjHTodORYh1t4xltBGIv3N9Nm0khQ3A1amAHG9PYaqDAhWdJYxitNA wE1TqpgRnkK3/kXnZY53Jf1nfwTeNuP/2KuFnHMHEoZVPG1JZmk+HoqG5JXwhB5GdFvs Zxy7nJMT1h0oXfPkyI7igaRn146B6+Aq4aX4GeyLmYQortFdtbCTnxU0sTqUijwes6qG QgPQ== 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 :content-transfer-encoding; bh=p8rqDRWOnwKnCpFlv14KT1UvUbw0sJGmcvPKkVafyTg=; b=sa2vfeS8bIhXn+UinGVo3H+O9U+Fj7IQuyo4vjErLq8P43sKPfb9XNyk0s3jkhVSR3 ksa6PTTMDVzSGBKd49Ye2cRz41Ux0tr+0P7RnF3HScvA+548i/X6x0zlql8Jsg74KOZ5 WRKV4axWLajfx81o3rGsflt655C2iGjUHnN8E5O6jJ7dpxY4luF33vcwXxLvXPaJwkKt DaU0xyJKH9QNJ8HcbfIrALqXcBcSOP14f3vo1jI7B3cnv9cZaw2eHPgkrjneokuf7w96 DUxAekikRwh3gNYeNntpSC/dV2UulzFEPdPRoXA+XmgZ6lQ4WJm9Joz5zD9W0R0y5Spr zcxQ== X-Gm-Message-State: AOAM531tbM3xvMbNctqpKIDPGmPYpUewHcaHQ1ZPxneFZn8c/LhAGiZn xrdkf5gKqp/GBg8Ahg1nVprrrvwa5UI= X-Google-Smtp-Source: ABdhPJw3ddCMa3Wq0g0OiWO9K4ZxvyR4XUIZKmkRnGHxtE+2lJaGk7I19gEJqeJUSncxZZKqqXYa+Q== X-Received: by 2002:a05:6808:1387:: with SMTP id c7mr13452939oiw.61.1613950015355; Sun, 21 Feb 2021 15:26:55 -0800 (PST) Received: from ?IPv6:2601:681:5500:dde0:299d:7cec:87c:8f52? ([2601:681:5500:dde0:299d:7cec:87c:8f52]) by smtp.gmail.com with ESMTPSA id m187sm1892687oif.43.2021.02.21.15.26.54 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 21 Feb 2021 15:26:54 -0800 (PST) Subject: Re: Script checking to see what database it's connected to To: pgsql-general@lists.postgresql.org References: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com> From: Rob Sargent Message-ID: <747b62a4-128c-093e-e212-252c34cea42c@gmail.com> Date: Sun, 21 Feb 2021 16:26:54 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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/