Re: psql and regex not like

From: Ron Johnson <ronljohnsonjr(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: psql and regex not like
Date: 2025-03-06 10:24:29
Message-ID: CANzqJaBaottzqJm6MjP_tuATRs81vtBanjn1Ba0sY==7FoUjXw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Mar 6, 2025 at 4:59 AM Dominique Devienne <ddevienne(at)gmail(dot)com>
wrote:

> On Thu, Mar 6, 2025 at 10:38 AM Ron Johnson <ronljohnsonjr(at)gmail(dot)com>
> wrote:
> > 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.)
>
> No answer to your question, but I'd argue it's moot, because it's not
> the right query in the first place :)
> It should be instead, IMHO, the one below, which should be OK in BASH
> syntax-wise. --DD
>
> select datname from pg_database WHERE datistemplate = false and
> datname <> 'postgres' order by 1
>

I already do that. This is part of a long chain of commands so I'm trying
to minimize the length of commands.

Anyway, it would be good to know the answer for any future queries that
need multiple exclusions.

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dominique Devienne 2025-03-06 10:41:42 Re: psql and regex not like
Previous Message Ron Johnson 2025-03-06 10:08:42 Re: [EXTERNAL] Re: Asking for OK for a nasty trick to resolve PG CVE-2025-1094 i