Re: Command Line option misunderstanding - Mailing list pgsql-novice

From Ken Dibble
Subject Re: Command Line option misunderstanding
Date
Msg-id 76561800-366d-4264-acb2-402bdc0cee25@beckydibble.com
Whole thread Raw
In response to Command Line option misunderstanding  (punch-hassle-guise@duck.com)
Responses Re: Command Line option misunderstanding
List pgsql-novice


On 12/2/24 15:36, David G. Johnston wrote:
On Mon, Dec 2, 2024 at 11:47 AM <punch-hassle-guise@duck.com> wrote:

----attempt 1--
psql -h anna -d GT7 -v v1=12 -c 'select :v1'
ERROR:  syntax error at or near ":"
LINE 1: select :v1

The variable got assigned correctly but it isn't in scope here due to the how -c is implemented, as the others noted.
 
----end attempt---

---attempt 2 ---
psql -h anna -d GT7 -vv1=1 -c 'select ":v1" '
ERROR:  column ":v1" does not exist
LINE 1: select ":v1"
You double-quoted the whole thing so it's taken as a column name.  Has no relevance to the problem at hand. 
---end attempt ---

--- atttempt 3----
psql -h anna -d GT7 -vv1=1 -c 'select :v1'
ERROR:  syntax error at or near ":"
LINE 1: select :v1
Redundant with 1 
---end attempt

---attempt 4----
sql -h anna -d GT7 -v "v1=1"  -c "select :v1"
ERROR:  syntax error at or near ":"
LINE 1: select ":v1"
Redundant with 1 and 3, putting quotes around the shell option value only makes explicit what is implicit/optional.  This is also a shell usage education issue, not psql specific. 
--- end attempt---

---attempt 5 ---
psql -h anna -d GT7 -v 'v1=1'  -c 'select ":v1" '
ERROR:  column ":v1" does not exist
LINE 1: select ":v1"
Redundant with 2 
----end attempt

--- attempt 6---
psql -h anna -d GT7 -v :v1=1  -c 'select ":v1" '
psql: error: invalid variable name: ":v1"
Shell command failed since you cannot name a variable with a leading colon, otherwise the chosen syntax for referencing a variable would be challenging 
---end attempt

--- attempt 7 ---
psql -h anna -d GT7 -v "v1"=1  -c 'select ":v1" '
ERROR:  column ":v1" does not exist
LINE 1: select ":v1"
Another redundant attempt using double-quotes 
--- end attempt

---attempt 8 ---
psql -h anna -d GT7 -v 'v1'=1  -c 'select ":v1" '
ERROR:  column ":v1" does not exist
LINE 1: select ":v1"
And again... 
--- end attempt ---

---attempt 9 ---
  psql -h anna -d GT7   -c '\set v1 12; select ":v1" '
... Not realizing how -c and meta-commands interplay (or don't in this case) really led you to try lots of stuff that cannot work but is unrelated to setting a variable.
--- end attempt ----

Skipping 10-17 as redundant variations on not reading how -c works.



---attempt 18---
psql -h anna -d GT7  -v  --set v1=12 -c 'select :v1 '
Password for user v1=12:
You can get odd errors when you don't following the syntax diagrams and shell command writing rules... 
---end attempt---


---attempt 19 ---
kdibble@thinkstation:~/development/gt7-scraper$ psql -h anna -d GT7  
--set v1=12 -c 'select :v1 '

ERROR:  syntax error at or near ":"
LINE 1: select :v1
Back to redundant with 1... 
---end attempt

--- attempt 20---
psql -h anna -d GT7   -v --set v1=12 -c 'select :v1 '
Password for user v1=12:
Again, not following syntax rules. 
--end attempt---

---attempt 21 ---
  psql -h anna -d GT7   -v v1  --set v1=12 -c 'select :v1 '

ERROR:  syntax error at or near ":"
LINE 1: select :v1
And back to redundant with 1 after figuring out syntax rules for -v/--set 
---end attempt

---attempt 22 ---
psql -h anna -d GT7   -v v1  --set =12 -c 'select :v1 '
psql: error: invalid variable name: ""
And now invalid shell syntax form for the set command. 
---end attempt---

---attempt 23---
psql -h anna -d GT7   -v v1 --set 12 -c 'select :v1 '

ERROR:  syntax error at or near ":"
LINE 1: select :v1

Redundant with 1 again... 
---end attempt

---attempt 24 ---
psql -h anna -d GT7   -v v1  --set v1=12 -c 'select :v1 '

ERROR:  syntax error at or near ":"
LINE 1: select :v1
And again... 
---end attempt---

---attempt 25---
psql -h anna -d GT7    -v --set v1=12 -c 'select :v1 '
Password for user v1=12:
Random stuff when inventing syntax again 
----

---attempt 26 ---
psql -h anna -d GT7    --set v1=12 -c 'select :v1 '

ERROR:  syntax error at or near ":"
LINE 1: select :v1
Redundant with 1 again... 
---end attempt

---attempt 27---
  psql -h anna -d GT7  -v v1="3" -c "select :v1"
+ psql -h anna -d GT7 -v v1=3 -c 'select :v1'

ERROR:  syntax error at or near ":"
LINE 1: select :v1
Redundant with 1 again... 
---end attempt

---attempt 28---
psql -h anna -d GT7  -v v1='3' -c "select :v1"
ERROR:  syntax error at or near ":"
LINE 1: select :v1
Redundant with again 
--- end attempt ---

---attempt 29---
psql -h anna -d GT7   -c '\set v1 12; select :v1 '
Didn't read how -c works 
---end attempt ---

---attempt 30 ---
psql -h anna -d GT7 -v --set v1:=12 -c 'select :v1 '
Password for user v1:=12:
Don't understand shell syntax for -v versus --set (repeat) 
---end attempt ---

---attempt 31 ---
psql -h anna -d GT7  --variable=v1=12 -c 'select :v1 '
ERROR:  syntax error at or near ":"
LINE 1: select :v1
Redundant with 1 again 
---end attempt---

---attempt 32---
psql -h anna -d GT7  --variable="v1=12" -c 'select :v1 '
ERROR:  syntax error at or near ":"
LINE 1: select :v1
Redundant with 1 again. 
---end attempt---

If short, you figured out a bunch of ways to write valid and invalid variable specifications. The invalid ones give you various different errors.  The correct ones all give you the same error since you cannot use a variable along with -c, which is documented in -c, not variables, since they get set just fine.
 
David J.

As  I stated, I was trying to figure out if there was a problem with the documentation.  Below is the documentation for -c and I cannot find anywhere it says you cannot use variables with it.  The only way that that could make sense is if declaring a variable is a psql specific feature and -v is hiding behind that in the man page.  I could find no place where it is documented that declaring a variable is a psql specific feature.  In addition, of what use would it be to declare a variable that could not be accessed without a command.  It wouldn't make sense (at least to me) to declare something that is going immediately out of scope before the next command executes or when the psql command (and hence the session) ends. 

In addition, declarations seems to work fine, it is assigning that fails.

If the variable did not exist or were out of scope it should generate an error, which it does not.

$ psql -h anna -d GT7 -c '\set v1  12; select :v1 '

autocommit on

$


Variable does not exist here.

$psql -h anna -d GT7 -c 'select :v1 '

autocommit on

ERROR:  syntax error at or near ":"
LINE 1: select :v1

$

One would hope that if the variable were out of scope it would generate an error.

Is there any explanation for this?


--man page extract --

-c command
       --command=command
           Specifies that psql is to execute the given command string,
           command. This option can be repeated and combined in any order with
           the -f option. When either -c or -f is specified, psql does not
           read commands from standard input; instead it terminates after
           processing all the -c and -f options in sequence.

           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. To achieve that, you could use
           repeated -c options or pipe the string into psql, for example:

               psql -c '\x' -c 'SELECT * FROM foo;'
   or

               echo '\x \\ SELECT * FROM foo;' | psql

           (\\ is the separator meta-command.)

           Each SQL command string passed to -c is sent to the server as a
           single request. Because of this, the server executes it as a single
           transaction even if the string contains multiple SQL commands,
           unless there are explicit BEGIN/COMMIT commands included in the
           string to divide it into multiple transactions. (See
           Section 55.2.2.1 for more details about how the server handles
           multi-query strings.)

           If having several commands executed in one transaction is not
           desired, 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


---end of extract---


pgsql-novice by date:

Previous
From: punch-hassle-guise@duck.com
Date:
Subject: Re: Command Line option misunderstanding
Next
From: Laurenz Albe
Date:
Subject: Re: Command Line option misunderstanding