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

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.

Related

postgres script silently pass without any result

i am trying to execute psql queries from the bash command line passing password in following format
set PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
Somehow my queries are not giving any results.i have tried to pass different queries or incorrect credentials but still script execute without any error.
If i don't pass password and try logging in to command prompt,everything works fine.
i want to check what basic thing i am missing above
Below command worked
PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
remove set at start of command fixed it

Psql output to file with column alias starting with number

I am running psql from command line and sending output to a file. It is a simple select statement on a view, but I am getting a syntax error when I have a column alias that starts with a number.
I ran the query in PgAdmin and it works (which makes me believe that this is some sort of issue with psql). I also tried adding a '_' to the beginning of the alias and that allows it to go through.
works: 'abc as "_1abc"'
doesn't work: 'abc as "1abc"'
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c
"SELECT abc as "1abc" From example.view
This is the error I get:
ERROR: syntax error at or near "1"
It is a problem with nested double quotes. You need to escape the inner ones.
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c "SELECT abc as \"1abc\" From example.view"

Pass command line args to sql (Postgres)

How can I pass command line args to sql files ran with psql (Postgres)?
i.e.
psql mydatabase < mysqlfile.sql arg1 arg2 arg3...
Is this possible?
Use variable interpolation feature in psql.
If you specify -v variable1=value1 or --set variable1=value1 parameter on command line, then :variable1 in the sql file will be replaced with corresponding text value.
Note: use standard-SQL quoted strings if you need quotes, spaces and so on.
Example:
echo "SELECT :arg1 FROM :arg2 LIMIT 10;" > script.sql
psql mydatabase -v arg1=relname -v arg2=pg_class < script.sql
psql mydatabase -v arg1="'some string' as label" -v arg2=pg_namespace < script.sql

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?

Postgres Vacuumdb sql

Im using my localhost to try and vacuumdb from java using create statement. I try to run the psql syntax in linux command line to verify if the syntax is indeed correct:
wsemp=# vacuumdb -d wsemp -z -v -h localhost -U jboss;
ERROR: syntax error at or near "vacuumdb"
LINE 1: vacuumdb -d wsemp -z -v -h localhost -U jboss;
I tried almost everything by removing some options and changing it to this:
wsemp=# vacuumdb --host=127.0.0.1 --port=5432 --dbname=wsemp --username=dbauser --analyze --verbose;
but the same error shows up. Any idea as to why?
vacuumdb is the command-line tool. The sql command is VACUUM. The syntax for options is a bit different: this is the documentation for it.
I'm guessing from the arguments you pass to vacuumdb that you'd want something like:
wsemp=# VACUUM (VERBOSE, ANALYZE);