PostgreSQL: pg_dump for a single table - postgresql

I'm beginner with PostgreSQL and doing backups using:
sudo -u postgres pg_dumpall > /~/postgreBackup.SQL
Works fine!
Now I want to backup a single table "TableName" in a scheme "SchemeName" and tried
sudo -u postgres pg_dump --table "SchemaName"."TableName" > /~/Dummy.SQL
pg_dump: no matching tables were found
How to get it working?

When you have case sensitive table and schema name you have to do the proper quoting of a table name. The below command should work fine as I have successfully executed it at my end.
Please make sure you are using the correct case sensitive name of database, schema and table in this command.
./pg_dump --dbname="myDatabase" --host=localhost --port=5432 --username=postgres --table='"MyScheme"."TableName 01"' --file=Dummy
OR
./pg_dump --dbname="myDatabase" --host=localhost --port=5432 --username=postgres --table='"MyScheme"."TableName 01"' > ~/Dummy.SQL

Related

pg_dump Command Issue

I have been using pg_dump for a while and every time I try to run the same script I seem to get issues. Not sure if it is user error or something to do with updating to Postgres 11.
Here is my command
pg_dump --dbname=postgresql://username:password#localhost:5432/DatabaseName --data-only --column-inserts -t "\"HoldingValuesTemp\"" > holdingValues.sql
This throws the error
pg_dump: too many command-line arguments (first is "HoldingValuesTemp\")
I think the issue has to do with the table name, it is case sensitive and is HoldingValuesTemp.
I tried to break it down into another pg_dump
pg_dump -d DatabaseName -p 5432 -U username --data-only --column-inserts -t "\"HoldingValuesTemp\"" > holdingValues.sql
Which gives the same error
So I also tried
pg_dump -d DatabaseName -p 5432 -U username --data-only --column-inserts -t '"HoldingValuesTemp"' > holdingValues.sql
after entering the password I get pg_dump: no matching tables were found
Any help would be appreciated.
My solution was more of a workaround than a solution.
The issue had to do with the table name, not sure why it was not finding that table but I assume it had to do with case sensitivity.
Solution:
Rename the table with a prefix that I looked up with a wildcard.
Table was "HoldingValuesTemp" I updated it to "ts_HoldingValuesTemp"
then ran the following command
pg_dump -d DatabaseName -p 5432 -U username --data-only --column-inserts -t 'ts_*' > holdingValues.sql
making it backup all tables that begin with "ts_"
Try to qualify the table name with the schema:
-t '"MySchema"."HoldingValuesTemp"'
There is also the possibility that you have a space character or similar in the table name.

Unable to restore psql database from pg_dump with a different username

I need to dump a postgres database from computer1 with postgres username1 and then restore it on computer2 with postgres username2. I keep running into the error that looks like the backup file wants to use username1:
When I run this on computer2:
psql dbname < backupname.pgsql
I get this error:
ERROR: role "username1" does not exist
I have tried:
// Dumping from computer1:
pg_dump dbname > backupname.sql
pg_dump dbname > backupname.pgsql
pg_dump -U username1 dbname -N topology -T spacial_ref_sys > backupname.pgsql
// Restoring on computer2:
psql dbname < backupname.pgsql
Is it the dumping or the restoring that needs to be modified to get past this?
The problem is with the dumping. With insight from this post I was able to resolve this using:
// On Computer1
pg_dump dbname -O -x > backupname.sql
// On Computer2
psql dbname < backupname.sql
The option flags used with pg_dump are:
-O <-- No owner
Do not output commands to set ownership of objects to match the original database
-x <-- No privileges
Prevent dumping of access privileges (grant/revoke commands)
See the PostgreSQL docs for pg_dump for more info on the option flags.
You don't need to cripple your dump by discarding owner/privileges. You can do it at restore time.
Use pg_restore with the --no-acl (and probably --no-owner) options:
-x
--no-privileges
--no-acl
Prevent restoration of access privileges (grant/revoke commands).
--no-owner
Do not output commands to set ownership of objects to match the
original database. By default, pg_restore issues ALTER OWNER or SET
SESSION AUTHORIZATION statements to set ownership of created schema
elements. These statements will fail unless the initial connection
to the database is made by a superuser (or the same user that owns
all of the objects in the script). With -O, any user name can be
used for the initial connection, and this user will own all the
created objects.
So something like:
pg_restore --no-privileges --no-owner -U postgres --clean ... $Your_sql_backup
If you are using pgAdmin then you can either remove the checkbox in DumpOptions #2 with Owner and otherwise remove the privilege like
--no-privileges and remove ownership like --no-password in the dump query like
/usr/bin/pg_dump --host localhost --port 5432 --username "postgres" --no-password --format custom --no-privileges --no-tablespaces --verbose --file "as" "databasename".
Also if you have constraints on the table then disable triggers also while creating the dump.
If you cannot create another backup of the database then the alternate way is to replicate the owner and roles of the dumped database to the new database. If you don't do that then you will get an error saying
'ACL does not exist' (not sure as faced it long back)

Postgres pg_restore command to restore complete schema

am using Postgres EnterpriseDB 9.5.0.5
I have taken a schema dump by using the below command
pg_dump -n 'schema1' db1 > schema1.dump
Now i want to restore it in different database (db2) what is the command i have to use.
i tried
pg_restore -d DB2 schema1.dump;
but it is showing error
pg_restore: [archiver] input file does not appear to be a valid archiver
You have two choices:
if you include no -f option for pg_dump, it creates a sql script so then restore using psql, not pg_restore
You could add -fc to create a custom binary/compressed format and then use pg_restore as you are trying to do.
However pg_restore mostly converts the archive to an SQL script, so it is not useful when you start with an sql script.
pg_dump -Fc mydb > db.dump
pg_restore -c -d mydb db.dump

I want to export a script for my database in postgres

I've created a database in pgAdmin, in which I've 1 public schema and 4 custom schema's. These schema contains several functions, sequences and Tables. Now I want to export a script that can create same database with the same structure without any data. Please help me with this....
You are looking for pg_dump with the (default) plain format.
If you can access to the command line, type this
pg_dump --create --clean --schema-only -U your_user -d db_name > file_name.sql
Then in the server you need to create same database run this
psql -U db_user -d db_name < file_name.sql
--clean option tries to drop all objects before creating them.
--create Begin the output with a command to create the database itself and reconnect to the created database.

How do I do a schema only backup and restore in PostgreSQL?

How do I take a schema level backup in PostgreSQL database and restore on the another database? Is there any single command available for this? For example, can I pg_dump and restore in single line?
pg_dump --schema=masters oldDB > masters1.sql
cat masters1.sql | psql newDB
or
in single command you can do by this
pg_dump oldDB --schema masters | psql -h localhost newDB;
Backup schema and restore it on system for postgresql as below:
Dump schema for database
pg_dump -s database_name > db.sql
Dump schema for specific table
pg_dump -s database_name -t table_name > db.sql
Restore backed up schema using below command
psql -d database_name -h localhost -U postgres < path/db.sql
-s or --schema-only to exclude data from dump
Documentation
What's wrong with the documentation?
Example from the manual:
To dump all schemas whose names start with east or west and end in
gsm, excluding any schemas whose names contain the word test:
$ pg_dump -n 'east*gsm' -n 'west*gsm' -N 'test' mydb > db.sql