Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tIELJ-00D4eg-Ft for pgsql-novice@arkaria.postgresql.org; Mon, 02 Dec 2024 21:52:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tIELF-003qAF-RS for pgsql-novice@arkaria.postgresql.org; Mon, 02 Dec 2024 21:52:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tIE25-003lCd-7y for pgsql-novice@lists.postgresql.org; Mon, 02 Dec 2024 21:32:42 +0000 Received: from sender4-of-o52.zoho.com ([136.143.188.52]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tIE21-000iRA-K2 for pgsql-novice@lists.postgresql.org; Mon, 02 Dec 2024 21:32:40 +0000 ARC-Seal: i=1; a=rsa-sha256; t=1733175154; cv=none; d=zohomail.com; s=zohoarc; b=NdNNAnmH2hF9ZHM8Vyi0sFIP8RIpvy6ctIPVI+02U78Y0pOpgUY0RJJCIo4wqXkSnypt9syWXIpulZq6+Cyycj8aQVZ6gtt3VMbWwDilsbhaQBDaTw52XiiCf/K1hp1DizOZYbVFMtTeLC7czMDIdJVbM1vMevGIeeVK5+//uig= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1733175154; h=Content-Type:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=/xMA53xJLFsJNnCK2fLOw0uaz+0tfX6zWbaFg4LnaiE=; b=aL8COn0pPvzJFGbdkDtRmIeK5tHYTFHSbeGvl9QEhFMVZPUzHd0ovh6Lylntbb8njlcKocbNLUuA0oreFmmlJTUER6ckMJXF4wk013eudnJ4EmvDSZlnw5mWmaAX1ZJ28GxXro3YD0MIdIs86EIF71RfpIHw1KJsmeNrIAe4is4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass smtp.mailfrom=ken@beckydibble.com; dmarc=pass header.from= Received: by mx.zohomail.com with SMTPS id 173317515176274.94890550394564; Mon, 2 Dec 2024 13:32:31 -0800 (PST) Content-Type: multipart/alternative; boundary="------------ZLeC4jCmwDNkMwgLivFy2U7S" Message-ID: <76561800-366d-4264-acb2-402bdc0cee25@beckydibble.com> Date: Mon, 2 Dec 2024 16:32:13 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Command Line option misunderstanding To: pgsql-novice@lists.postgresql.org References: <487DB217-EA37-4139-AB97-B61B04ECAEA7.1@smtp-inbound1.duck.com> Content-Language: en-US From: Ken Dibble In-Reply-To: X-ZohoMailClient: External List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------ZLeC4jCmwDNkMwgLivFy2U7S Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/2/24 15:36, David G. Johnston wrote: > On Mon, Dec 2, 2024 at 11:47 AM 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 > *DuckDuckGo* did not detect any trackers. More > > > > Deactivate > > > > On Mon, Dec 2, 2024 at 11:47 AM 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 <


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


--------------ZLeC4jCmwDNkMwgLivFy2U7S--