Postgres dump specific table with a capital letter - postgresql

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.

Related

Is it possible to extract only tables starting with a specific name in pg_dump in Postgres?

Among the hundreds of tables in the postgres DB, only the structure dump of tables starting with a specific name is found.
Example
the name of the table
abc
abc_hello
abc_hi
def
def_hello
def_hi
If you think there is
I want to dump only tables starting with abc*.
pg_dump abc.txt -Fp -t abc* -s DBName
However, it was not recognized because the amount of tables was too large.
it answered,
pg_dump: error: too many command-line arguments (first is "DBName")
but this command works fine
pg_dump abc_hello.txt -Fp -t abc_hello -s DBName
How can I pick them out?
Your main mistake is that you didn't consider that * also is a special character for the shell. If you run
pg_dump -t abc* mydatabase
the shell will replace abc* with the names of all files in the current directory that start with abc, so that your command will actually be something like
pg_dump -t abcfile1 abcfile2 abcfile3 mydatabase
which is syntactically not correct and will make pg_dump complain.
You have to protect the asterisk from being expanded by the shell with single quotes:
pg_dump -t 'abc*' mydatabase
The other error in your command line is that you forgot the -f in -f abc.txt.

How to escape quotes in a docker postgres command

I have the following command that I'm running (renamed some variables):
docker exec docker_name sh -c 'psql dbname -U joeadmin -c "update table set field='really_longstringwithabunf3493829#########=';"'
When I run this, it will throw the following error:
ERROR: syntax error at or near ";"
LINE 1: ...longstringwithabunf3493829#########=;
^
How can I escape or retain the single quotes so this will work. Also, if this is just a horrible way of approaching this, I'm open to other suggestions. The use case is making changes to a db on a docker container before exporting data out of it.
Working with strings can be a bit ugly at times with Docker. In your case, I would suggest using double-quotes first, then escape inner double-quotes with \:
docker exec docker_name sh -c "psql dbname -U joeadmin -c \"update table set field='really_longstringwithabunf3493829#########=';\""
A cleaner option would be to create an environment variable:
STR="really_longstringwithabunf3493829#########="
docker exec docker_name sh -c "psql dbname -U joeadmin -c \"update table set field='${STR}';\""
Bear in mind that string interpolation happens in the host OS, not in the container (unless you escape the dollar-sign with \${STR}
Still cleaner would just be to create a file and then copy it into the container:
echo "update table set field='really_longstringwithabunf3493829#########=';" > ~/myfile.txt
docker cp ~/myfile.txt docker_name:/tmp
docker exec docker_name sh -c "psql dbname -U joeadmin -f /tmp/myfile.txt"

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"

Postgres from unix shell out put not appear in Log

I am writing a batch job for Postgres for first time. I have return ".sh" file, which has a command with out any out put in the log or console.
Code
export PGPASSWORD=<password>
psql -h <host> -p <port> -U <user> -d <database> --file cleardata.sql > log\cleardata.log 2>&1
What I did at cammond line
su postgres
and run ./cleardatasetup.sh
Nothing is happening.
Please note : When I try psql command in Unix command line, I am getting message as some SQL exception which is valid.
Can any one please help me in this regard.
You probably wanted to create log/cleardata.log but you have a backslash where you need a slash. You will find that the result is a file named log\cleardata.log instead.
The backslash is just a regular character in the file's name, but it's special to the shell, so you'll need to quote or escape it to (unambiguously) manipulate it from the shell;
ls -l log\\cleardata.log # escaped
mv 'log\cleardata.log' log/cleardata.log # quoted

pg_dump: too many command line arguments

what is wrong with this command:
pg_dump -U postgres -W admin --disable-triggers -a -t employees -f D:\ddd.txt postgres
This is giving error of too many command-line arguments
Looks like its the -W option. There is no value to go with that option.
-W, --password force password prompt (should happen automatically)
If you want to run the command without typing is a password, use a .pgpass file.
http://www.postgresql.org/docs/9.1/static/libpq-pgpass.html
For posterity, note that pg_dump and pg_restore (and many other commands) cannot process long hyphens that word processors create. If you are cut-pasting command lines from a word processor, be sure it hasn't converted your hyphens to something else in editing. Else you will get command lines that look correct but hopelessly confuse the argument parsers in these tools.
pg_dump and pg_restore need to ask password on commandline, if you put it command, they always give "too many command-line arguments" error. You can use below for setting related environment variable in commandline or batch file:
"SET PGPASSWORD=<password>"
so that you are not asked to enter password manually in your batch file. They use given environment variable.
Instead of passing password with -W flag start with setting temporary variable for postgres:
PGPASSWORD="mypass" pg_dump -U postgres--disable-triggers -a -t employees -f D:\ddd.txt postgres
-W -> will prompt for a password
to take full DB dump
use some thing like
pg_dump -h 192.168.44.200 -p 5432 -U postgres -W -c -C -Fc -f C:\MMM\backup10_3.backup DATABASE_NAME
I got this from copy-pasting, where 1 of the dashes were different.
Was: –-host= (first dash i a "long" dash)
Corrected to --host= solved it
Another option is to add ~/.pgpass file with content like this:
hostname:port:database:username:password
read more here
Additionally, if you don't want password prompt, use connection string directly.
pg_dump 'postgresql://<username>:<password>#localhost:5432/<dbname>'
So, combination with options in original question,
pg_dump 'postgresql://postgres:<password>#localhost:5432/postgres' --table='"employees"' --format='t' --file='D:\ddd.txt' --data-only --disable-triggers
(Don't forget to use quotes when you have letter-casing issues)
reference:
https://www.postgresql.org/docs/current/app-pgdump.html
Postgres dump specific table with a capital letter
2021-11-30, pg v12, windows 10
pg_dump -U postgres -W -F t postgres > C:\myfolder\pg.tar
-U "postgres" as username,
-W to prompt for psd,
-F t means format is .tar,
> C:\myfolder\pg.tar is the destination path and filename