pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Syntax error when combining --set and --command has me stumped
6+ messages / 4 participants
[nested] [flat]

* Syntax error when combining --set and --command has me stumped
@ 2022-07-28 19:40 Ron <ronljohnsonjr@gmail.com>
  2022-07-28 19:47 ` Re: Syntax error when combining --set and --command has me stumped David G. Johnston <david.g.johnston@gmail.com>
  2022-07-28 23:36 ` Re: Syntax error when combining --set and --command has me stumped Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Ron @ 2022-07-28 19:40 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


$ alias psql12
alias psql12='/usr/lib/postgresql/12/bin/psql -p5433'

This works ask expected:

$ psql12 --set num=42 -ac "\echo :num"
echo :num
42

And so does this:

$ psql12 --set num=42
psql (12.11 (Ubuntu 12.11-1.pgdg18.04+1))
Type "help" for help.

postgres=# select :num;
  ?column?
----------
        42
(1 row)


But trying to use a variable (both with and without single quotes) in a 
--command statement other than "\echo" throws a syntax error at the colon:

$ psql12 --set num=42 -ac "select :num;"
select :num;
ERROR:  syntax error at or near ":"
LINE 1: select :num;
                ^
$ psql12 --set num=42 -ac "select :'num';"
select :'num';
ERROR:  syntax error at or near ":"
LINE 1: select :'num';
                ^

What secret sauce am I missing to get this to work?

-- 
Angular momentum makes the world go 'round.





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

* Re: Syntax error when combining --set and --command has me stumped
  2022-07-28 19:40 Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
@ 2022-07-28 19:47 ` David G. Johnston <david.g.johnston@gmail.com>
  2022-07-28 19:50   ` Re: Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: David G. Johnston @ 2022-07-28 19:47 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thu, Jul 28, 2022 at 12:40 PM Ron <ronljohnsonjr@gmail.com> wrote:

> What secret sauce am I missing to get this to work?
>

Given that the documentation says:

"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."

I don't see how you can do anything to make that work.

David J.

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

* Re: Syntax error when combining --set and --command has me stumped
  2022-07-28 19:40 Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
  2022-07-28 19:47 ` Re: Syntax error when combining --set and --command has me stumped David G. Johnston <david.g.johnston@gmail.com>
@ 2022-07-28 19:50   ` Ron <ronljohnsonjr@gmail.com>
  2022-07-29 09:05     ` Re: Syntax error when combining --set and --command has me stumped Gianni Ceccarelli <dakkar@thenautilus.net>
  0 siblings, 1 reply; 6+ messages in thread

From: Ron @ 2022-07-28 19:50 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 7/28/22 14:47, David G. Johnston wrote:
> On Thu, Jul 28, 2022 at 12:40 PM Ron <ronljohnsonjr@gmail.com> wrote:
>
>     What secret sauce am I missing to get this to work?
>
>
> Given that the documentation says:
>
> "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."

And all this time, I was looking in the |--set=/|assignment|/| section of 
the psql doc page...

https://www.postgresql.org/docs/12/app-psql.html

>
> I don't see how you can do anything to make that work.
>
> David J.
>

-- 
Angular momentum makes the world go 'round.

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

* Re: Syntax error when combining --set and --command has me stumped
  2022-07-28 19:40 Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
  2022-07-28 19:47 ` Re: Syntax error when combining --set and --command has me stumped David G. Johnston <david.g.johnston@gmail.com>
  2022-07-28 19:50   ` Re: Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
@ 2022-07-29 09:05     ` Gianni Ceccarelli <dakkar@thenautilus.net>
  2022-08-02 13:07       ` Re: Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Gianni Ceccarelli @ 2022-07-29 09:05 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

If you can use bash, or set up some redirections from whatever you're
using to execute ``psql``, you can do::

  $ psql somedb --set num=42 <<<'select :num'
  Timing is on.
  Expanded display is used automatically.
  Line style is unicode.
  Border style is 2.
  ┌──────────┐
  │ ?column? │
  ├──────────┤
  │       42 │
  └──────────┘
  (1 row)

  Time: 0.517 ms

or (more classically)::

  echo 'select :num' | psql somedb --set num=42

-- 
	Dakkar - <Mobilis in mobile>
	GPG public key fingerprint = A071 E618 DD2C 5901 9574
	                             6FE2 40EA 9883 7519 3F88
	                    key id = 0x75193F88






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

* Re: Syntax error when combining --set and --command has me stumped
  2022-07-28 19:40 Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
  2022-07-28 19:47 ` Re: Syntax error when combining --set and --command has me stumped David G. Johnston <david.g.johnston@gmail.com>
  2022-07-28 19:50   ` Re: Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
  2022-07-29 09:05     ` Re: Syntax error when combining --set and --command has me stumped Gianni Ceccarelli <dakkar@thenautilus.net>
@ 2022-08-02 13:07       ` Ron <ronljohnsonjr@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Ron @ 2022-08-02 13:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 7/29/22 04:05, Gianni Ceccarelli wrote:
> If you can use bash, or set up some redirections from whatever you're
> using to execute ``psql``, you can do::
>
>    $ psql somedb --set num=42 <<<'select :num'
>    Timing is on.
>    Expanded display is used automatically.
>    Line style is unicode.
>    Border style is 2.
>    ┌──────────┐
>    │ ?column? │
>    ├──────────┤
>    │       42 │
>    └──────────┘
>    (1 row)
>
>    Time: 0.517 ms
>
> or (more classically)::
>
>    echo 'select :num' | psql somedb --set num=42

Since my process uses the same variable in multiple bash statements, I 
decided to use a bash environment variable.

-- 
Angular momentum makes the world go 'round.





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

* Re: Syntax error when combining --set and --command has me stumped
  2022-07-28 19:40 Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
@ 2022-07-28 23:36 ` Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Adrian Klaver @ 2022-07-28 23:36 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org

On 7/28/22 12:40, Ron wrote:
> 
> $ alias psql12
> alias psql12='/usr/lib/postgresql/12/bin/psql -p5433'
> 
> This works ask expected:
> 
> $ psql12 --set num=42 -ac "\echo :num"
> echo :num
> 42
> 
> And so does this:
> 
> $ psql12 --set num=42
> psql (12.11 (Ubuntu 12.11-1.pgdg18.04+1))
> Type "help" for help.
> 
> postgres=# select :num;
>   ?column?
> ----------
>         42
> (1 row)
> 
> 
> But trying to use a variable (both with and without single quotes) in a 
> --command statement other than "\echo" throws a syntax error at the colon:
> 
> $ psql12 --set num=42 -ac "select :num;"
> select :num;
> ERROR:  syntax error at or near ":"
> LINE 1: select :num;
>                 ^
> $ psql12 --set num=42 -ac "select :'num';"
> select :'num';
> ERROR:  syntax error at or near ":"
> LINE 1: select :'num';
>                 ^
> 
> What secret sauce am I missing to get this to work?


 From here:

https://www.postgresql.org/docs/current/app-psql.html

-c command

...

Because of this behavior, putting more than one SQL command in a single 
-c string often has unexpected results. It's better to use repeated -c 
commands or feed multiple commands to psql's standard input, either 
using echo as illustrated above, or via a shell here-document, for example:

psql <<EOF
\x
SELECT * FROM foo;
EOF


So:

echo '\set num 42 \\ SELECT :num;' | psql -d test -U aklaver
Null display is "NULL".
  ?column?
----------
        42

or:

psql -d test -U aklaver <<EOF
 > \set num 42
 > SELECT :num;
 > EOF
Null display is "NULL".
  ?column?
----------
        42
(1 row)




-- 
Adrian Klaver
adrian.klaver@aklaver.com





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


end of thread, other threads:[~2022-08-02 13:07 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-07-28 19:40 Syntax error when combining --set and --command has me stumped Ron <ronljohnsonjr@gmail.com>
2022-07-28 19:47 ` David G. Johnston <david.g.johnston@gmail.com>
2022-07-28 19:50   ` Ron <ronljohnsonjr@gmail.com>
2022-07-29 09:05     ` Gianni Ceccarelli <dakkar@thenautilus.net>
2022-08-02 13:07       ` Ron <ronljohnsonjr@gmail.com>
2022-07-28 23:36 ` Adrian Klaver <adrian.klaver@aklaver.com>

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