psql command-line variable interpolation leads to syntax error - postgresql

From the psql command documentation, the --variable command described as making variables available for substitution with :name syntax. However,
psql --variable=var="'hello'" -c 'select :var'
...results in a syntax error:
ERROR: syntax error at or near ":"
LINE 1: select :var

This works correctly if the query text is fed in on stdin in bash:
psql --variable=var="'hello'" <<<"select :var"
Or by using POSIX sh:
psql --variable=var="'hello'" <<<'EOF'
select :var
EOF

Related

PostgreSQL copy command gives error for temporary table

I am trying to run the below command on the unix console :
env 'PGOPTIONS=-c search_path=admin -c client_min_messages=error' psql -h hostname -U user -p 1111 -d platform -c "CREATE TEMP TABLE admin.tmp_213 AS SELECT * FROM admin.file_status limit 0;\copy admin.tmp_213(feed_id,run_id,extracted_date,file_name,start_time,file_size,status,crypt_flag) FROM '/opt/temp/213/list.up' delimiter ',' csv;UPDATE admin.file_status SET file_size = admin.tmp_213.file_size FROM admin.tmp_213 A WHERE admin.file_status.feed_id = A.feed_id and admin.file_status.file_name = A.file_name;"
I am getting the below error:
ERROR: syntax error at or near "\"
LINE 1: ...* FROM admin.file_status limit 0;\copy admi...
If I use the above command without a backslash before COPY, it gives me the below error:
ERROR: cannot create temporary relation in non-temporary schema
I am doing the above to implement the solution as mentioned here:
How to update selected rows with values from a CSV file in Postgres?
Your first error is because metacommands like \copy cannot be combined in the same line as regular commands when given with -c. You can give two -c options, with one command in each instead.
The second error is self-explanatory. You don't get to decide what schema your temp table goes to. Just omit the schema.

syntax error at or near ":" when running parametrized query from shell

I'm trying to run a parameterized query from shell.
But when I run:
p='some stuff'
psql -d "dbname" -v v1="$p" -c "SELECT * FROM table WHERE name=:'v1'"
I'm getting the following error:
ERROR: syntax error at or near ":"
Meanwhile:
psql -d "dbname" -v v1="$p" -c "\echo :'v1'"
works normally. (returns as expected: 'some stuff')
You cannot use the variable defined in -v in -c command (see below). Try passing the command into the standard input:
psql -d "dbname" -v v1="$p" <<< "SELECT * FROM table WHERE name=:'v1'"
From the document:
-c command
--command command
...
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.
...
-v does set the psql's internal variable, which is psql-specific features. That's why you got the syntax error.

PostgreSQL unterminated quoted identifier

I have this groovy code which drops a remote postgres schema from commandline:
def dropSchema = "psql --dbname=postgres://$user:$pass#localhost:$port/$targetDb -c \"DROP SCHEMA ${applicationName}_${uid} CASCADE;\"".execute()
This code is working fine when it's run on a windows machine, but when it's on a Linux distribution, it gives me these errors:
psql: warning: extra command-line argument "appName_uid" ignored
psql: warning: extra command-line argument "CASCADE;"" ignored
ERROR: unterminated quoted identifier at or near ""DROP"
LINE 1: "DROP
^
Does anyone know how to fix this ?
Thanks.
Never ever use a string with .execute() like "ls 'my fancy file'".execute(). It splits on whitespace and that is most likely never what you want (same as ["ls", "'my", "fancy", "file'"].execute() for that example).
Also .execute() runs the command via the regular exec of your OS -- not the shell. So quoting or other things, that needs to be done for a shell command actually make things worse - since no shell is involved to interpret your intention.
Instead use an array, where all params are their own (don't quote for a shell, that is never used)
[
"psql",
"--dbname=postgres://$user:$pass#localhost:$port/$targetDb",
"-c", "DROP SCHEMA ${applicationName}_${uid} CASCADE;"
].execute()
If you prefer to reuse an existing shell command, then run it with a shell:
["/bin/sh", "-c", "psql ... -c \"DROP ...\" ..."].execute()
Here you have to quote for the shell, as it is executed like a shell command.

psql extra command line argument

Can someone tell me why I get the error extra command line argument here? When I use -f and give it the full path to the sql file it works fine. I would like to use a relative path instead so I was trying to use the \ir command.
psql -c \c postgresql://docker:1234/nbt?ssl=true -U admin -v username='user73291' -v recipeId=2 -c \ir '../../../resources/sql/myfile.sql'
Error:
psql: warning: extra command-line argument "../../../resources/sql/myfile.sql" ignored
ERROR: syntax error at or near "ir"
LINE 1: ir
^
Thanks!
\i is a meta-command to be used in the psql command line, not in the shell command line. What is the problem with -f?

Get error codes while using psql

When I run an SQL script using psql.exe I am not getting error codes when an error occurs. Is there any way to get the psql error codes?
I tried setting VERBOSITY to 'verbose' like this, but no use:
\set VERBOSITY 'verbose'
I am using psql of version psql (PostgreSQL) 8.4.2.
Get rid of the quotes:
test=# \set VERBOSITY verbose
test=# select broken;
ERROR: 42703: column "broken" does not exist
LINE 1: select broken;
^
LOCATION: transformColumnRef, parse_expr.c:766