pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
gexec from command prompt?
4+ messages / 4 participants
[nested] [flat]

* gexec from command prompt?
@ 2023-01-12 17:25 Ron <ronljohnsonjr@gmail.com>
  2023-01-12 17:33 ` Re: gexec from command prompt? Pavel Stehule <pavel.stehule@gmail.com>
  2023-01-12 18:04 ` Re: gexec from command prompt? Alvaro Herrera <alvherre@alvh.no-ip.org>
  0 siblings, 2 replies; 4+ messages in thread

From: Ron @ 2023-01-12 17:25 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Postgresql 12.11

This might be more of a bash question, or it might be a psql vs engine problem.

I want to run this query using psql from a bash prompt:
select format('SELECT ''%s'', MIN(part_date) FROM %s;', table_name, table_name)
from dba.table_structure
order by table_name\gexec

Thus, I added an extra back

$ psql sides -atXc "select format('SELECT ''%s'', MIN(part_date) FROM %s;', 
table_name, table_name) from dba.table_structure order by table_name limit 
5\\gexec"
select format('SELECT ''%s'', MIN(part_date) FROM %s;', table_name, 
table_name) from dba.table_structure order by table_name limit 5\gexec
ERROR:  syntax error at or near "\"
LINE 1: ...) from dba.table_structure order by table_name limit 5\gexec

Removing "\\exec" from the statement, and appending -c "\\gexec" to the psql 
command technically worked, but did not run the commands.

-- 
Born in Arizona, moved to Babylonia.





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

* Re: gexec from command prompt?
  2023-01-12 17:25 gexec from command prompt? Ron <ronljohnsonjr@gmail.com>
@ 2023-01-12 17:33 ` Pavel Stehule <pavel.stehule@gmail.com>
  2023-01-12 18:41   ` Re: gexec from command prompt? David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Pavel Stehule @ 2023-01-12 17:33 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

čt 12. 1. 2023 v 18:25 odesílatel Ron <ronljohnsonjr@gmail.com> napsal:

> Postgresql 12.11
>
> This might be more of a bash question, or it might be a psql vs engine
> problem.
>
> I want to run this query using psql from a bash prompt:
> select format('SELECT ''%s'', MIN(part_date) FROM %s;', table_name,
> table_name)
> from dba.table_structure
> order by table_name\gexec
>
> Thus, I added an extra back
>
> $ psql sides -atXc "select format('SELECT ''%s'', MIN(part_date) FROM
> %s;',
> table_name, table_name) from dba.table_structure order by table_name limit
> 5\\gexec"
> select format('SELECT ''%s'', MIN(part_date) FROM %s;', table_name,
> table_name) from dba.table_structure order by table_name limit 5\gexec
> ERROR:  syntax error at or near "\"
> LINE 1: ...) from dba.table_structure order by table_name limit 5\gexec
>
> Removing "\\exec" from the statement, and appending -c "\\gexec" to the
> psql
> command technically worked, but did not run the commands.
>

I don't know why, but \g* commands don't work from the -c option. But in
this case it is not necessary

you can psql -c "xxx" | psql

Regards

Pavel


>
> --
> Born in Arizona, moved to Babylonia.
>
>
>

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

* Re: gexec from command prompt?
  2023-01-12 17:25 gexec from command prompt? Ron <ronljohnsonjr@gmail.com>
  2023-01-12 17:33 ` Re: gexec from command prompt? Pavel Stehule <pavel.stehule@gmail.com>
@ 2023-01-12 18:41   ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: David G. Johnston @ 2023-01-12 18:41 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thu, Jan 12, 2023 at 10:34 AM Pavel Stehule <pavel.stehule@gmail.com>
wrote:

>
> čt 12. 1. 2023 v 18:25 odesílatel Ron <ronljohnsonjr@gmail.com> napsal:
>
>>
>> Removing "\\exec" from the statement, and appending -c "\\gexec" to the
>> psql
>> command technically worked, but did not run the commands.
>>
>
> I don't know why, but \g* commands don't work from the -c option. But in
> this case it is not necessary
>
>
Well, the -c option states:

command must be either a command string that is completely parsable by the
server (i.e., it contains no psql-specific features), or a single backslash
command. Thus you cannot mix SQL and psql meta-commands within a -c option.

Thus any meta-command that interacts with server-parsed SQL is rendered
useless in -c

David J.

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

* Re: gexec from command prompt?
  2023-01-12 17:25 gexec from command prompt? Ron <ronljohnsonjr@gmail.com>
@ 2023-01-12 18:04 ` Alvaro Herrera <alvherre@alvh.no-ip.org>
  1 sibling, 0 replies; 4+ messages in thread

From: Alvaro Herrera @ 2023-01-12 18:04 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On 2023-Jan-12, Ron wrote:

> Postgresql 12.11
> 
> This might be more of a bash question, or it might be a psql vs engine problem.
> 
> I want to run this query using psql from a bash prompt:
> select format('SELECT ''%s'', MIN(part_date) FROM %s;', table_name, table_name)
> from dba.table_structure
> order by table_name\gexec

Yeah, what I use in these cases is something like

echo "select format('SELECT ''%s'', MIN(part_date) FROM %s;', table_name, table_name)
from dba.table_structure
order by table_name \gexec" | psql -f-


-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"El hombre nunca sabe de lo que es capaz hasta que lo intenta" (C. Dickens)





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


end of thread, other threads:[~2023-01-12 18:41 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-01-12 17:25 gexec from command prompt? Ron <ronljohnsonjr@gmail.com>
2023-01-12 17:33 ` Pavel Stehule <pavel.stehule@gmail.com>
2023-01-12 18:41   ` David G. Johnston <david.g.johnston@gmail.com>
2023-01-12 18:04 ` Alvaro Herrera <alvherre@alvh.no-ip.org>

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