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 1lDzJV-0004vc-8Y for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 00:43:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lDzJU-0004ej-2I for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 00:43:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDzJT-0004eb-OT for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 00:42:59 +0000 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lDzJN-0003Lw-Hb for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 00:42:59 +0000 Received: by mail-ot1-x334.google.com with SMTP id g6so6370602otk.11 for ; Sun, 21 Feb 2021 16:42:53 -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-transfer-encoding:content-language; bh=z/EVvV9cyuOu0hjgZR5Kbcd16pV0KPi0cpbxefVbso4=; b=f4T5pcKdjKbKzlDOkTiOtaYZZyTx/qpikHnnfDU6ghRWRUaSnsAcXB7tSs8kMnr7bS CwKU1dwo00Nfrwsrgjh6OBnBBpdJtVXEOv7BuQWeWVYWMxl6ZlGOJ7eoWFW6hj00+qoM OwG1iK+AkVJrSBXSQT+TDJXij53hhsWEs7OsDeQlaZyQuexDvjxxxW7sORKpI55u0iAM hoqlsHJ8vFry5qrFL7yHT3sEheE/rXIevtrNhMLx+GAT+moC6Fm18R6Oi/ZibImUZxz1 bbbb1E/jK23U6lfu0sHXhwc/iZHJMI2Fqj5SHyBc4ly+LGVtzapYGJHNMbBnTISdxXu1 jcHA== 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-transfer-encoding :content-language; bh=z/EVvV9cyuOu0hjgZR5Kbcd16pV0KPi0cpbxefVbso4=; b=dBBALATGGPdFMGVQD7GFZHV5AuF5AADdgp7GG4qf9AUE4pIaVTdn5+qCevu6HpY97n tOLYEC2zGqRHlUw4A+PNypou38znof+XzVuaY7Pgd42jGovEFM6h38PviKx9bHiKbKyP aY3sVPpSn8oJ/YwurF5YGya6DGJHUgQt0jTu3EeNXdxKbUNlhhTR7R5hmMAoHdeeNX36 MAi7iAJJgmeXZlxx3p66tWXvW7SYqRVJewBkWm3aDknsKdOrNgiZwhfsUIENdXiTZNRZ wkisViC1Rv66zj36guaQh+f3lyJ9IWMtnTVhytkkn0wUIeUFBbvtOBg78zMnlJ0UpD66 pUig== X-Gm-Message-State: AOAM531EIaByTe+D0fwvcSrQvnYxZWaSU6gNGNP3s0Ts0aF3lxbI1Wsp c8ay7CWG4pHcxWOLVi28jyrb2A6Q0QWH3w== X-Google-Smtp-Source: ABdhPJzW2nG8WY1swTVJXF2mqfOZP5RngS0t+1mv31FL74c2LqpK8XSzElMU71VzycCnsnbrMtpPEA== X-Received: by 2002:a9d:7b44:: with SMTP id f4mr3273425oto.240.1613954571645; Sun, 21 Feb 2021 16:42:51 -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 v19sm1792480ots.48.2021.02.21.16.42.50 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 21 Feb 2021 16:42:51 -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> <747b62a4-128c-093e-e212-252c34cea42c@gmail.com> From: Ron Message-ID: Date: Sun, 21 Feb 2021 18:42:50 -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: <747b62a4-128c-093e-e212-252c34cea42c@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.