public inbox for [email protected]  
help / color / mirror / Atom feed
psql and regex not like
2+ messages / 2 participants
[nested] [flat]

* psql and regex not like
@ 2025-03-06 09:37 Ron Johnson <[email protected]>
  2025-03-13 21:48 ` Re: psql and regex not like Renan Alves Fonseca <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Ron Johnson @ 2025-03-06 09:37 UTC (permalink / raw)
  To: pgsql-general

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 <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: psql and regex not like
  2025-03-06 09:37 psql and regex not like Ron Johnson <[email protected]>
@ 2025-03-13 21:48 ` Renan Alves Fonseca <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Renan Alves Fonseca @ 2025-03-13 21:48 UTC (permalink / raw)
  To: Ron Johnson <[email protected]>; +Cc: pgsql-general

Another dirty hack:

MAGIC=\! psql -Xc "select datname from pg_database WHERE datname $MAGIC~
'template|postgres' ORDER BY datname;"

Em qui., 6 de mar. de 2025 às 10:38, Ron Johnson <[email protected]>
escreveu:

> 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 <Redacted>, and butter sauce.
> Don't boil me, I'm still alive.
> <Redacted> lobster!
>


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-03-13 21:48 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-06 09:37 psql and regex not like Ron Johnson <[email protected]>
2025-03-13 21:48 ` Renan Alves Fonseca <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox