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"
Related
I am trying to setup a shell script to insert data from .csv files into postgreSQL. As a first test I just tried rto do it in terminal command, and I am running into an issue because my table has a '$' in its name and I was not able to come up with the proper way to escape it so far.
My command look like this :
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY ts_dev.tbl$trm_secndry_readings from 'FILEPATH' with delimiter ',' ;"
I have made several test, with this command, if tablename='test1' the command works, but if tablename='prefix&test1' then the command fails as terminal is trying to parse what is after '$' as a variable name
So for example :
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY schtest.prefix$name from 'FILEPATH' with delimiter ',' ;"
ERROR: relation "schtest.prefix" does not exist
I'm using Windows powershell.
In both POSIX-compatible shells such as bash and in PowerShell, $ is a metacharacter, which - in unquoted use or inside "..." - requires escaping in order to be interpreted verbatim (in order to be passed through to a command as-is).
However, the escape character differs between these two shells:
For POSIX-compatible shells it is \:
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY ts_dev.tbl\$trm_secndry_readings from 'FILEPATH' with delimiter ',' ;"
For PowerShell it is ` (the so-called backtick; see about_Special_Characters):
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY ts_dev.tbl`$trm_secndry_readings from 'FILEPATH' with delimiter ',' ;"
you can add escaped double quotes around the table name
psql -d trm -h localhost -p 5432 -U postgres -c "\COPY
\"ts_dev.tbl$trm_secndry_readings\" from 'FILEPATH' with delimiter ',' ;"
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
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.
Suppose I created a sequence in postgresql:
CREATE SEQUENCE my_seq;
I store the below line in an sql file get_seq.sql
SELECT last_value FROM my_seq;
$SUDO psql -q -d database_bame -f get_seq.sql
How do I get the int number returned by SELECT into bash and use it?
You can capture the result of a command using the VAR=$(command) syntax:
VALUE=$(psql -qtAX -d database_name -f get_seq.sql)
echo $VALUE
The required psql options mean:
-t only tuple
-A output not unaligned
-q quiet
-X Don't run .psqlrc file
Try:
LAST_VALUE=`echo "SELECT last_value FROM my_seq;" | psql -qAt -d database_name`
I am trying to perform a postgres dump of a specific table using -t. However, the table has a capital letter in it and I get a "No matching tables were found." I tried using quotations and double quotations around the table name but they did not work. How can I get pg to recognize the capitals? Thanks!
pg_dump -h hostname dbname -t tableName > pgdump.sql
Here is the complete command to dump your table in plain mode:
pg_dump --host localhost --port 5432 --username "postgres" --role "postgres" --format plain --file "complete_path_file" --table "schema_name.\"table_name\"" "database_name"
OR you can just do:
pg_dump -t '"tablename"' database_name > data_base.sql
Look to the last page here: Documentation
The above solutions do not work for me under Windows 7 x64. PostgreSQL 9.4.5. But this does, at last (sigh):
-t "cms.\"FooContents\""
either...
pg_dump.exe -p 8888 --username=user -t "cms.\"FooContents\"" basdb
...or...
pg_dump.exe -p 8888 --username=user -table="cms.\"FooContents\"" basdb
Inside a cmd window, I had to put three (!) double quotes around the table name if it countains upper case letters.
Example
pg_dump -t """Colors""" database > database.colors.psql
This worked for me:
pg_dump -f file.sql -U user -t 'schema.\"Table\"' database
As part of a node script I had to surround with single and double quotes, e.g.
` ... --table 'public."IndexedData"'`
The accepted solution worked in a bash console, but not as part of a node script, only the single quote approach.
Thanks to #Dirk Zabel suggestion, the following worked for me:
Windows 10 CMD
pg_dump -d "MyDatabase" -h localhost -p 5432 -U postgres --schema=public -t """TableName""" > TableName.sql
Bash
pg_dump -d "MyDatabase" -h localhost -p 5432 -U postgres --schema=public -t "\"TableName\"" > TableName.sql
Powershell
the good (shortest)
& 'C:\Program Files\PostgreSQL\12\bin\pg_dump.exe' -d db_name -t '\"CasedTableName\"'
the bad (requires --%)
& 'C:\Program Files\PostgreSQL\12\bin\pg_dump.exe' --% -d db_name -t "\"CasedTableName\""
the ugly (requires `")
& 'C:\Program Files\PostgreSQL\12\bin\pg_dump.exe' -d db_name -t "\`"CasedTableName\`""
The main point of confusion for me was the absolute necessity of having \" in there. I assumed that maybe there was a weird bug in the way powershell or psql was parsing the arguments, but it turns out it's explained in the docs:
Some native commands expect arguments that contain quote characters. Normally, PowerShell's command line parsing removes the quote character you provided. The parsed arguments are then joined into a single string with each parameter separated by a space. This string is then assigned to the Arguments property of a ProcessStartInfo object. Quotes within the string must be escaped using extra quotes or backslash (\) characters.
And of course ProcessStartInfo.Arguments Remarks tells us:
To include quotation marks in the final parsed argument, triple-escape each mark.