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 1lDzt2-0005ra-DZ for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 01:19:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lDzt1-00036q-52 for pgsql-general@arkaria.postgresql.org; Mon, 22 Feb 2021 01:19:43 +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 1lDzt0-00036j-RS for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 01:19:42 +0000 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lDzsu-0003iI-IH for pgsql-general@lists.postgresql.org; Mon, 22 Feb 2021 01:19:42 +0000 Received: by mail-pg1-x536.google.com with SMTP id n10so9240209pgl.10 for ; Sun, 21 Feb 2021 17:19:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:date:in-reply-to:message-id :mime-version; bh=3xck2Q4tjsovKjRzXMLQzi72rSwOieZB3yrmaqtI1Nc=; b=hI6toNdB6cwDi8Kfx9KTapvi/0ONA1ZGopeHtuy3Nz1llu81PLTwsU58kSDVOshOX5 1X1AN6MaUbfKgvd6uIcRd3znsGFBly/mvMCQpgOu3DafN56qdSAG4hGmT1iJhSvfndjC oYKsQdV+sAr8tm06TVclYQfR369ZjKl+gsFLVBl3B72wziC8N0OFF0SJsqos6udSl4dK uLUpOsxnVnG0LlnEYrB/Z466Spn/dk2GhXokMqbyrtPsRpQ9ui5V7nVeZH1fi42Jb3wS SmzunXPDMqFhByMqirXLSRXvegTUzCv6vDyBvPLa1yx5vwjTFfOdfY5C+K35k9+YnklS jFWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:subject:date :in-reply-to:message-id:mime-version; bh=3xck2Q4tjsovKjRzXMLQzi72rSwOieZB3yrmaqtI1Nc=; b=H+lL6gaWePpWhxA7dRlKaF0VojUSCr6bNRh9y1m5/RIrihgJ3oNssITwYblnjNycme K8Ge/3rROGp8iQSyb2hJc1cYnqEBAGGt9OFGAkEqsEPJNZZNOlp7rLf+j9PDh+5FaV8W /SEw/NitnNXt00Bq9n3wQzC07q2VmcuUy3bIanq/m+77K1VG0y5499N6TBl81GPeggu1 R5JCXidf7gaPMZANA03U5eWoCV3JvlwZSeEFy2Ku2ibIbE9mQPxZxZVJa2ruINONaPNc mu8L1gr8QvtsyYYWR2s6ziI+OIzOpi14AfdAPbitAScdF2Bsscaj5qmspAn6/jBKxx2H 4lIw== X-Gm-Message-State: AOAM531SXjrbBC8Bmg+rixZXvVz+Aexh86R5Z5bhSagEGolCqqXHUIuD 6Bp6zyPCw6GKjDp9qn2QWElQGFMXhOc= X-Google-Smtp-Source: ABdhPJxevb21oG4lVTyGq0EMOjdkg0wR0jXNANAKgZPAtxczDERr9f68OGxz7vugyn5EE+CKq/y5rA== X-Received: by 2002:aa7:9462:0:b029:1e0:c1d:750a with SMTP id t2-20020aa794620000b02901e00c1d750amr19157713pfq.25.1613956773975; Sun, 21 Feb 2021 17:19:33 -0800 (PST) Received: from tim-desktop (106-69-124-219.dyn.iinet.net.au. [106.69.124.219]) by smtp.gmail.com with ESMTPSA id u10sm16310473pjr.27.2021.02.21.17.19.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Feb 2021 17:19:33 -0800 (PST) References: <0437e8e0-5686-9a10-b204-0b9df86e27ef@gmail.com> <747b62a4-128c-093e-e212-252c34cea42c@gmail.com> <5e45b234-9390-e394-dc31-4dc3f7dbffdc@gmail.com> User-agent: mu4e 1.5.8; emacs 27.1.91 From: Tim Cross To: pgsql-general@lists.postgresql.org Subject: Re: Script checking to see what database it's connected to Date: Mon, 22 Feb 2021 12:01:49 +1100 In-reply-to: <5e45b234-9390-e394-dc31-4dc3f7dbffdc@gmail.com> Message-ID: <87a6rw7vby.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Rob Sargent writes: >>> >>> 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. >> > Isn't it a toss-up with putting the check in every sql script? > Or make it /really/ fancy: use proper arg parsing; check for existence > of the sql script; add a usage function; split stdout/stderr... No end > of fun. that would be my approach. A general purpose 'launcher' script that does argument checking, logging and reporting. Stick it in your bin directory and then call that instead of psql directly. Now all your SQL scripts are just DDL/DML statements. Nice thing is you can do it in whatever scripting language your most comfortable with - bash, perl, ruby, python, whatever and it is available for whatever project your working on. -- Tim Cross