Thread: Issue with psqlrc with command line.

Issue with psqlrc with command line.

From
saurabh shelar
Date:
Hi All,

I am facing issue while executing psqlrc alias commands via. "psql -c" option.

Please find below details:

-bash-4.2$ cat .psqlrc 
\set testing 'select * from test;'

postgres=# select version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16), 64-bit
(1 row)

postgres=# :testing
 id 
----
  1

However, it is not working with the command line.

-bash-4.2$ /opt/PostgreSQL/9.6/bin/psql -c ":testing"
ERROR:  syntax error at or near ":" at character 1
STATEMENT:  :testing
ERROR:  syntax error at or near ":"
LINE 1: :testing

As per the document from PG-9.6 it is possible. 

Apologies, If I am getting wrong. 

Re: Issue with psqlrc with command line.

From
"David G. Johnston"
Date:
On Wednesday, August 29, 2018, saurabh shelar <saurabhshelar5@gmail.com> wrote:
As per the document from PG-9.6 it is possible. 

You cannot use psql variables with -c

“ 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.”


David J.

Re: Issue with psqlrc with command line.

From
saurabh shelar
Date:
Hi David,

Thank you for the swift response.

However, could you please confirm if the below scenario is expected. 

- included the alias in the file.
- And passed the file with psql and it worked.

-bash-4.2$ cat test 
:testing

-bash-4.2$ /opt/PostgreSQL/9.6/bin/psql -f test 
 id 
----
  1


On Wed, Aug 29, 2018 at 8:44 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Wednesday, August 29, 2018, saurabh shelar <saurabhshelar5@gmail.com> wrote:
As per the document from PG-9.6 it is possible. 

You cannot use psql variables with -c

“ 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.”


David J.

Re: Issue with psqlrc with command line.

From
"David G. Johnston"
Date:
On Wednesday, August 29, 2018, saurabh shelar <saurabhshelar5@gmail.com> wrote:
Hi David,

Thank you for the swift response.

However, could you please confirm if the below scenario is expected. 

- included the alias in the file.
- And passed the file with psql and it worked.

-bash-4.2$ cat test 
:testing

-bash-4.2$ /opt/PostgreSQL/9.6/bin/psql -f test 
 id 
----
  1

 Yes.  It’s only -c that is constrained. (There may be some interplay with whether psqlrc is consulted in various situations as well...)

David J.

Re: Issue with psqlrc with command line.

From
saurabh shelar
Date:
Hi David,

Thank you for the help.

I was just got confused with the below line mentioned in the document. However, it seems it is still the same behaviour (i.e --no-psqlrc).

"Before PostgreSQL 9.6, the -c option implied -X (--no-psqlrc); this is no longer the case."

On Wed, Aug 29, 2018 at 8:54 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Wednesday, August 29, 2018, saurabh shelar <saurabhshelar5@gmail.com> wrote:
Hi David,

Thank you for the swift response.

However, could you please confirm if the below scenario is expected. 

- included the alias in the file.
- And passed the file with psql and it worked.

-bash-4.2$ cat test 
:testing

-bash-4.2$ /opt/PostgreSQL/9.6/bin/psql -f test 
 id 
----
  1

 Yes.  It’s only -c that is constrained. (There may be some interplay with whether psqlrc is consulted in various situations as well...)

David J.

Re: Issue with psqlrc with command line.

From
"David G. Johnston"
Date:
On Wed, Aug 29, 2018 at 8:31 AM, saurabh shelar <saurabhshelar5@gmail.com> wrote:
Hi David,

Thank you for the help.

I was just got confused with the below line mentioned in the document. However, it seems it is still the same behaviour (i.e --no-psqlrc).

"Before PostgreSQL 9.6, the -c option implied -X (--no-psqlrc); this is no longer the case."

Whether psqlrc is being processed or not is immaterial to the behavior you are seeing because -c will not resolve the variable reference prior to sending the command to the server.

If you have other non-variable definitions in psqlrc that could affect the -c command (or its output - like \pset instructions) those will now be applied before sending the command whereas before they would not be.

(admittedly I'm not reading the source code nor have experimented to draw this conclusion)

David J.

Re: Issue with psqlrc with command line.

From
saurabh shelar
Date:
Hi David,

Got your point. 

Thank you for the help.

On Wed, Aug 29, 2018 at 9:07 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Wed, Aug 29, 2018 at 8:31 AM, saurabh shelar <saurabhshelar5@gmail.com> wrote:
Hi David,

Thank you for the help.

I was just got confused with the below line mentioned in the document. However, it seems it is still the same behaviour (i.e --no-psqlrc).

"Before PostgreSQL 9.6, the -c option implied -X (--no-psqlrc); this is no longer the case."

Whether psqlrc is being processed or not is immaterial to the behavior you are seeing because -c will not resolve the variable reference prior to sending the command to the server.

If you have other non-variable definitions in psqlrc that could affect the -c command (or its output - like \pset instructions) those will now be applied before sending the command whereas before they would not be.

(admittedly I'm not reading the source code nor have experimented to draw this conclusion)

David J.