Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tq7gC-00HMyU-GO for pgsql-general@arkaria.postgresql.org; Thu, 06 Mar 2025 09:38:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tq7gA-0053AU-QI for pgsql-general@arkaria.postgresql.org; Thu, 06 Mar 2025 09:38:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tq7gA-00538y-Ck for pgsql-general@lists.postgresql.org; Thu, 06 Mar 2025 09:38:10 +0000 Received: from mail-oo1-xc35.google.com ([2607:f8b0:4864:20::c35]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tq7g8-001Gss-26 for pgsql-general@postgresql.org; Thu, 06 Mar 2025 09:38:09 +0000 Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-5fd19b469b0so104930eaf.3 for ; Thu, 06 Mar 2025 01:38:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741253888; x=1741858688; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Wmpx9CpKHv83UcXNBFdPBYGzYHSM2uTPHPxX/0qm7GI=; b=LS4F/j+PHH9Ryo0e6LTwfJJeQqngyXjYPgDlZ50Zju0jx30tuESPdLJce6v3uVsBST regKUCdrpBhqAHGy3yBK1mVdhk2T9Xhv7auEtwGYnIyVzgZYrSivoJ9VYWkqoakHEm/+ Rb2M7oPhdcJGW0BLm6W+yV4is/3ikbqjjoG7Elhd/0SWrW8gHK+9uKnbbGt3EOsmBUIB xgl5ofmK6HZSFvwOzVuIT8a3IVUTXFTrkQkPynNWpiimsHYtB/IMU8iiqEtFcZyWzgka oO/E2E4x3Ixm/Ueo940FMLzOEzgpNxlGbWJUZRQUlw90JgKO9JJicwEUdjfWx7GxPM8d qOwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741253888; x=1741858688; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Wmpx9CpKHv83UcXNBFdPBYGzYHSM2uTPHPxX/0qm7GI=; b=SQEXI8GLBZKpSoIcbWBYjtBl5Jdycw15jIAwakilKs2j6pFExrRAhaC0HKDL3PDLo2 R9bSgHsO/cJWalwkPRGjLMdwa0P52mzCn7jQnilcI2bGiCsKxPaNepNG1eAqvLxX+VWa 2cT4B5MC2CY9kjqe1rv1uFYyJguALCE36geOoadZ6meMAlVPf8FENfL7dvEQMlIyktNo Zh+69H5UlUsCEBh8yLwqQpi2FzdTGHgPTB+/laBx1asZYTvkXwAM1wlvZN9qE9NCmzib u32IcTXjYW4oVpc2sxf97+qR+JwcD5N6GU0UEDV0hNsSlw+0qCtSlRzhKc1W2G/2EZit tCtA== X-Gm-Message-State: AOJu0YzUjx+wqMzFv2Rth5u94zZnogLONQg3fPTRr2CvKgjB/0TtPTq8 jg5jbeACQQSoFN8NSUXd+bxGTiEqw1yfhQ6SRG34328A7EmWjRDDmUP1aArFkEXPhxabf3YkvS1 xdKlS9IFsBo1tPQ+FPpkcdsHFZrWkkHif X-Gm-Gg: ASbGncuGdCQr7SKi07PujVn2hR0vW3LVsM1RGPdXBXCjghwonvKl8J0TAKuzQbrMvpM czkMHF1lybmsubhUNb4odBaeF1WE2CEYn210MWhvMYGMMA6eXwVX9YUS9tJiIQKj+6YGKo62kYr 0StcFgVwFaDd8JpHrCQgMYiw/TVxPE68qdZfVNoANpcVN0mkqOPwhQqg6Bvl+M X-Google-Smtp-Source: AGHT+IEobr0Lg7MVFJTTDQUNLA2R3jMOikWSbmLwQlpOVmLk3xOz7EIBD+NRq2dHk+mDJNqgZbEZSFDrWrpnUxNaBWk= X-Received: by 2002:a05:6870:7d8a:b0:296:e46a:6e5e with SMTP id 586e51a60fabf-2c21cca3d28mr3610469fac.21.1741253887709; Thu, 06 Mar 2025 01:38:07 -0800 (PST) MIME-Version: 1.0 From: Ron Johnson Date: Thu, 6 Mar 2025 04:37:56 -0500 X-Gm-Features: AQ5f1JpPV-AM4-WiKnqrS_SDV2Tq6N5QvJ44U9u9CKEeFRKNblSSEQTpotCnjuQ Message-ID: Subject: psql and regex not like To: pgsql-general Content-Type: multipart/alternative; boundary="00000000000070da46062fa943ce" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000070da46062fa943ce Content-Type: text/plain; charset="UTF-8" This statement runs great from the psql prompt. Does exactly what I want. select datname from pg_database WHERE datname !~ 'template|postgres' ORDER BY datname; But it doesn't work so well from the bash prompt. Not escaping the "!" generates a bunch of garbage, while escaping throws an sql syntax error. psql -Xc "select datname from pg_database WHERE datname \!~ 'template|postgres' ORDER BY datname;" ERROR: syntax error at or near "\" What's the magic syntax? (Yes, I could create a view and then query the view, but I'm going to be running this remotely against dozens of servers, so I don't want to have to create dozens of views, then need to recreate them every time I want to change the query.) -- Death to , and butter sauce. Don't boil me, I'm still alive. lobster! --00000000000070da46062fa943ce Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
This statement runs great from the psql prompt.=C2=A0= Does exactly what I want.
= select datname from pg_database WHERE datname !~ 'template|postgres'= ; ORDER BY datname;

But it doesn't work= so well from the bash prompt.=C2=A0 Not escaping the "!" generat= es a bunch of garbage, while escaping throws an sql syntax=C2=A0error.

psql -Xc "select datname= from pg_database WHERE datname \!~ 'template|postgres' ORDER BY da= tname;"
ERROR: =C2=A0syntax error at or near "\"

What's the magic=C2=A0syntax?
(Yes, I could create a view and then query the view, but I'= m going to be running this remotely against dozens of servers, so I don'= ;t want to have to create dozens of views,=C2=A0then need to recreate them = every time I want to change the=C2=A0query.)

--
Death to <= ;Redacted>, and butter sauce.
Don't boil me, I'm still alive= .
<Redacted> lobster!
--00000000000070da46062fa943ce--