Ignoring a table in pg_dump and restore - postgresql

This is what I currently do to copy a database from my local machine to a remote server.
Dump local database:
pg_dump dbname --clean -U postgres > dumpfile
Restore remote database:
psql --single-transaction dbname -U postgres < dumpfile
This does a perfect replication.
How do I modify this to ignore particular table names, both in the source and destination?
This will be useful for tables that log website visits. I want to retain my existing remote visit logs, while ignoring my local "visit" logs (which is just me visiting my own website locally).

Using the -t switch, you can be selective about what tables to include:
pg_dump <your switches> -t my_schema.awesome* -f backup.bat postgres
Which will only include those tables.
Likewise, the -T switch will do the opposite -- dump everything but the tables you specified:
pg_dump <your switches> -T my_schema.lame* -f backup.bat postgres
You can use each switch multiple times as well:
pg_dump <your switches> \
-t my_schema.awesome* \
-t my_schema.amazing* \
-t my_schema.great -f backup.bat postgres
When you restore, it won't bother trying to restore something it didn't back up, so that part should be handled somewhat natively.

Related

How to compare a schema defined in a file with actual schema running on DB?

I'm running a PostgreSQL DB and want to compare the schema defined in schema.sql with the schema that is created on the DB on a regular basis. I.e. to verify that no manual changes on the remote are applied or that schema.sql actually reflects what's running on the DB.
What I'm currently doing is roughly:
create a dump from schema.sql using pg_virtualenv:
$ pg_virtualenv sh -c 'psql -U $PGDATABASE -f schema.sql
$ pg_dump --schema-only --no-comments --no-owner --no-privileges --schema=public -U $PGDATABASE -f schema_local.sql'
create a dump from the remote
$ pg_dump --schema-only --no-comments --no-owner --no-privileges --schema=public -U $PGREMOTE -f schema_upstream.sql
strip out comments, SET, emtpy lines, etc. and diff them
$ diff -u <(grep -Ev '^--|^SET|^$$' schema_local.sql) <(grep -Ev '^--|^SET|^$$' schema_upstream.sql)
That works reasonably well and I can easily spot schema changes in the diff if they occur. But the solution seems a bit hacky and I wonder if there's a better way to achieve the same thing?

Cannot restore Postgresql databases got "database already exists" error

I have take backup by pg_dumpall > test.out
and test.out successfully generated, hence backup completed.
I have used command psql -f test.out postgres for restore
But got following errors with restoring backup:
databases already exists
relation "products" already exists
duplicate key value violates unique constraint "products_pkey"
I actually want to replace the data in the existing db with backup. How to do that?
The problem is that the database you're trying to restore already exists.
You can run a DROP DATABASE database_name command that will delete your existing database and then you can run your test.out file.
Or you can run pgdumpall --clean > test.out and then run the resulting file. The clean flag will make the resulting files have the DROP DATABASE command in them.
Do you use the bellow command ?
psql -h localhost -U [login role] database_name -f /home/database.backup
I think a flow like this might help, because we don't want drop the database each time we call the backup file.
First, we need to create a backup file using the --format=custom [-Fc] to restore it using pg_restore. We can use a connection string postgresql://<user>:<pass>#localhost:5432/<dbname> and replace <user>, <pass>, and <dbname> with your information.
pg_dump -v -Fc \
postgresql://<user>:<pass>#localhost:5432/<dbname> \
> db-20211122-163508.sql
To restore we will call it using --clean [-c] and --create [-C] to drop the database before restoring. Replace <user>, <host>, <port>, and <dbname> with your information.
pg_restore -vcC \
-U <user> \
-h <host> \
-p <port> \
-d <dbname> \
< db-20211122-163508.sql
This way you don't need to use clean when you create the backup file.

how to restore a postgresql database from a file? [duplicate]

Locally, I use pgadmin3. On the remote server, however, I have no such luxury.
I've already created the backup of the database and copied it over, but is there a way to restore a backup from the command line? I only see things related to GUI or to pg_dumps.
There are two tools to look at, depending on how you created the dump file.
Your first source of reference should be the man page pg_dump as that is what creates the dump itself. It says:
Dumps can be output in script or
archive file formats. Script dumps are
plain-text files containing the SQL
commands required to reconstruct
the database to the state it was
in at the time it was saved. To
restore from such a script, feed it to
psql(1). Script files can be used
to reconstruct the database even
on other machines and other
architectures; with some modifications
even on other SQL database products.
The alternative archive file formats
must be used with pg_restore(1) to
rebuild the database. They allow
pg_restore to be selective about what
is restored, or even to reorder the
items prior to being restored. The
archive file formats are designed to
be portable across architectures.
So depends on the way it was dumped out. If using Linux/Unix, you can probably figure it out using the excellent file(1) command - if it mentions ASCII text and/or SQL, it should be restored with psql otherwise you should probably use pg_restore.
Restoring is pretty easy:
psql -U username -d dbname < filename.sql
-- For Postgres versions 9.0 or earlier
psql -U username -d dbname -1 -f filename.sql
or
pg_restore -U username -d dbname -1 filename.dump
Check out their respective manpages - there's quite a few options that affect how the restore works. You may have to clean out your "live" databases or recreate them from template0 (as pointed out in a comment) before restoring, depending on how the dumps were generated.
create backup
pg_dump -h localhost -p 5432 -U postgres -F c -b -v -f
"/usr/local/backup/10.70.0.61.backup" old_db
-F c is custom format (compressed, and able to do in parallel with -j N) -b is including blobs, -v is verbose, -f is the backup file name.
restore from backup
pg_restore -h localhost -p 5432 -U postgres -d old_db -v
"/usr/local/backup/10.70.0.61.backup"
important to set -h localhost - option
You might need to be logged in as postgres in order to have full privileges on databases.
su - postgres
psql -l # will list all databases on Postgres cluster
pg_dump/pg_restore
pg_dump -U username -f backup.dump database_name -Fc
switch -F specify format of backup file:
c will use custom PostgreSQL format which is compressed and results in smallest backup file size
d for directory where each file is one table
t for TAR archive (bigger than custom format)
-h/--host Specifies the host name of the machine on which the server is running
-W/--password Force pg_dump to prompt for a password before connecting to a database
restore backup:
pg_restore -d database_name -U username -C backup.dump
Parameter -C should create database before importing data. If it doesn't work you can always create database eg. with command (as user postgres or other account that has rights to create databases) createdb db_name -O owner
pg_dump/psql
In case that you didn't specify the argument -F default plain text SQL format was used (or with -F p). Then you can't use pg_restore. You can import data with psql.
backup:
pg_dump -U username -f backup.sql database_name
restore:
psql -d database_name -f backup.sql
POSTGRESQL 9.1.12
DUMP:
pg_dump -U user db_name > archive_name.sql
put the user password and press enter.
RESTORE:
psql -U user db_name < /directory/archive.sql
put the user password and press enter.
Below is my version of pg_dump which I use to restore the database:
pg_restore -h localhost -p 5432 -U postgres -d my_new_database my_old_database.backup
or use psql:
psql -h localhost -U postgres -p 5432 my_new_database < my_old_database.backup
where -h host, -p port, -u login username, -d name of database
Backup and restore with GZIP
For larger size database this is very good
backup
pg_dump -U user -d mydb | gzip > mydb.pgsql.gz
restore
gunzip -c mydb.pgsql.gz | psql dbname -U user
https://www.postgresql.org/docs/14/backup-dump.html
This worked for me:
pg_restore --verbose --clean --no-acl --no-owner --host=localhost --dbname=db_name --username=username latest.dump
Backup: $ pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}
Restore: $ psql -U {user-name} -d {desintation_db} -f {dumpfilename.sql}
try this:
psql -U <username> -d <dbname> -f <filename>.sql
Restore DB psql from .sql file
Backup & Restore
This is the combo I'm using to backup, drop, create and restore my database (on macOS and Linux):
sudo -u postgres pg_dump -Fc mydb > ./mydb.sql
sudo -u postgres dropdb mydb
sudo -u postgres createdb -O db_user mydb
sudo -u postgres pg_restore -d mydb < ./mydb.sql
Misc
-Fc will compress the database (Format custom)
List PostgreSQL users: sudo -u postgres psql -c "\du+"
You may want to add hostname and date to ./mydb.sql, then change it by:
./`hostname`_mydb_`date +"%Y%m%d_%H%M"`.sql
If you create a backup using pg_dump you can easily restore it in the following way:
Open command line window
Go to Postgres bin folder. For example: cd "C:\ProgramFiles\PostgreSQL\9.5\bin"
Enter the command to restore your database. For example: psql.exe -U postgres -d YourDatabase -f D:\Backup\.sql
Type password for your postgres user
Check the restore process
I didnt see here mentions about dump file extension (*.dump).
This solution worked for me:
I got a dump file and needed to recover it.
First I tried to do this with pg_restore and got:
pg_restore: error: input file appears to be a text format dump. Please use psql.
I did it with psql and worked well:
psql -U myUser -d myDataBase < path_to_the_file/file.dump
1. Open the Terminal.
2. Backup your database with following command
your postgres bin -> /opt/PostgreSQL/9.1/bin/
your source database server -> 192.168.1.111
your backup file location and name -> /home/dinesh/db/mydb.backup
your source db name -> mydatabase
/opt/PostgreSQL/9.1/bin/pg_dump --host '192.168.1.111' --port 5432 --username "postgres" --no-password --format custom --blobs --file "/home/dinesh/db/mydb.backup" "mydatabase"
3. Restore mydb.backup file into destination.
your destination server -> localhost
your destination database name -> mydatabase
Create database for restore the backup.
/opt/PostgreSQL/9.1/bin/psql -h 'localhost' -p 5432 -U postgres -c "CREATE DATABASE mydatabase"
Restore the backup.
/opt/PostgreSQL/9.1/bin/pg_restore --host 'localhost' --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"
1) Open psql terminal.
2) Unzip/ untar the dump file.
3) Create an empty database.
4) use the following command to restore the .dump file
<database_name>-# \i <path_to_.dump_file>
To restore a dump file
psql -d [Dbname] -U [UserName] -p 5432 < [FileLocation]
To restore a .SQL file
pg_restore -U [Username] -d [Dbname] -1 [FileLocation]
If you get user authentication errors, go to the file pg_hba.conf which is in PSQL/data folder in your program files, and change the "METHOD" to "Trust".
Restart you psql serive in windows services(Win + R --> services.msc).
try:
pg_restore -h localhost -p 5432 -U <username> -d <dbname> -1 <filename>
Restoring a postgres backup file depends on how did you take the backup in the first place.
If you used pg_dump with -F c or -F d you need to use pg_restore otherwise you can just use
psql -h localhost -p 5432 -U postgres < backupfile
9 ways to backup and restore postgres databases
As below link said, you can use psql command for restoring the dump file:
https://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTORE
psql dbname < infile
if you need to set username just add the username after the command like:
psql dbname < infile username
Sorry for the necropost, but these solutions did not work for me. I'm on postgres 10. On Linux:
I had to change directory to my pg_hba.conf.
I had to edit the file to change method from peer to md5 as stated here
Restart the service: service postgresql-10 restart
Change directory to where my backup.sql was located and execute:
psql postgres -d database_name -1 -f backup.sql
-database_name is the name of my database
-backup.sql is the name of my .sql backup file.
Try to see if the following commands can help you:
sudo su - yourdbuser
psql
\i yourbackupfile
If you have a backup SQL file then you can easily Restore it.
Just follow the instructions, given in the below
1. At first, create a database using pgAdmin or whatever you want (for example my_db is our created db name)
2. Now Open command line window
3. Go to Postgres bin folder. For example: cd "C:\ProgramFiles\PostgreSQL\pg10\bin"
4. Enter the following command to restore your database: psql.exe -U postgres -d my_db -f D:\Backup\backup_file_name.sql
Type password for your postgres user if needed and let Postgres to do its work. Then you can check the restore process.
The shortest way with no password prompt
psql "postgresql://<db_user>:<db_pass>#<ip>:<port>/<db_name>" < "backup.sql"
If you are using Windows OS
psql.exe "postgresql://<db_user>:<db_pass>#<ip>:<port>/<db_name>" < "backup.sql"
I was having authentication problems running pg_dump, so I moved my dump file
mv database_dump /tmp
into the temp directory and then ran
su -u postgres
cd /tmp
pg_restore database_dump
If you have a large database dump, you may just want to create another directory where your current user and the postgres user can access and putting the database dump file into that.
Backup==>
Option1: To take backup along with password in cmd
1.PGPASSWORD="mypassword" pg_dump -U postgres -h localhost --inserts mydb>mydb.sql
Option2: To take backup without password in cmd
2. pg_dump -U postgres -h localhost --inserts mydb>mydb.sql
Option3: To take backup as gzip(if database is huge)
3. pg_dump -U postgres -h localhost mydb --inserts | gzip > mydb.gz
Restore:
1. psql -h localhost -d mydb -U postgres -p 5432 < mydb.sql
This solution only works for Windows.
First, ensure you have already added the postgres bin folder to the "Path" environment variable (in my case this folder is C:\Program Files\PostgreSQL\12\bin).
Then, open the Windows command interpreter (cmd), go to the folder where you have the .sql file and execute this command:
pg_restore -U userName -d database-1 backupfile.sql
For example:
pg_restore -U sam -d SamDataBase -1 SamDataBaseBackup.sql
(It can ask you for the password of the user so ensure to type it correctly and then click enter)
Pura vida!
If you have created a new database named mydb, To restore a .sql dump to that database with psql,
psql --file=dump.sql --username=postgres --host=localhost --port=5432 mydb
the password will be prompted by psql
The connection options are
-h, --host=HOSTNAME database server host or socket directory (default: "/var/run/postgresql")
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "xyz")
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
If you are using docker, this answer may be helpful.
Start the container
docker start <postgres_container_id>
Access bash inside container
docker exec -it <postgres_container_id> bash
Copy the .tar backup file to docker container (In another window)
docker cp postgres_dump.tar <postgres_container_id>:/
Restore the backup
pg_restore -c -U <postgres-user> -d <password> -v "postgres_dump.tar" -W
Enter password
Save and restore the exact same state with compressed dump
Other answers gave all the key bits separately, but hopefully this will provide be the "just works save and restore to exact state" command pair.
Dump to file mydb.psql:
PGPASSWORD=mypassword pg_dump -U my_username -h localhost mydb -Fc -f mydb.psql
Restore:
PGPASSWORD=mypassword pg_restore -U my_username -h localhost \
--clean -d mydb -v mydb.psql
Some of the flags:
-Fc: Format Compressed, as opposed to plaintext.
file tmp.psql says:
tmp.psql: PostgreSQL custom database dump - v1.14-0
--clean: destroy the target DB before restoring it, thus returning to the exact same pristine state.
Any data created after the dump will be lost.
PGPASSWORD, -U and -h can of course be modified depending on your login method, e.g. without PGPASSWORD you're prompted for a password, and none of those are needed if you set up peer auth locally.
Tested on Ubuntu 22.04, PostgreSQL 14.5.
If you want to backup your data or restore data from a backup, you can run the following commands:
To create backup of your data, go to your postgres \bin\ directory like C:\programfiles\postgres\10\bin\ and then type the following command:
pg_dump -FC -U ngb -d ngb -p 5432 >C:\BACK_UP\ngb.090718_after_readUpload.backup
To restore data from a backup, go to your postgres \bin\ directory like C:\programfiles\postgres\10\bin\ and then type below command:
C:\programFiles\postgres\10\bin> pg_restore -Fc -U ngb -d ngb -p 5432 <C:\ngb.130918.backup
Please make sure that the backup file exists.
Follow these 3 steps :
start postgres server - sudo systemctl start postgresql
enable same - sudo systemctl enable postgresql
restore command - pg_restore -h localhost -p 5432 -U postgres -d old_db
assuming that the dump is there in the same directory
Links :
https://www.postgresqltutorial.com/postgresql-restore-database
https://askubuntu.com/questions/50621/cannot-connect-to-postgresql-on-port-5432

Which pgdump format is best for small storage size and fast restore?

This a first time foray in PostgreSQL backups (db dumps) and I've been researching the different pgdump formats, other pgdump options, and pgdumpall. For a Postgres beginner looking at taking an hourly dump (will overwrite previous dump) of two databases that contain table triggers and two different schemas in each db, what would be the backup format and options to easily achieve the following:
Small file size (single file per db or ability to choose which db to restore)
Easy to restore as clean db (with & without same db name[s])
Easy to restore on different server (user maybe different)
Triggers are disabled on restore and re-enabled after restore.
Include example commands to backup and restore.
Any other helpful pgdump/pgrestore suggestions welcome.
This command will create a small dmp file which includes only structure of the dattabase - tabels, columns, triggers, views etc.. (This command will just take few minutes)
pg_dump -U "dbuser" -h "host" -p "port" -F c -b -v -f ob_`date +%Y%m%d`.dmp dbname
**ex:** pg_dump -U thames -h localhost -p 5432 -F c -b -v -f ob_`date +%Y%m%d`.dmp dbname
This command will take the backup of complete database
pg_dump -h localhost -U "dbuser" "dbname" -Fc > "pathfilename.backup"
**ex:** pg_dump -h localhost -U thames thamesdb - Fc > "thamesdb.backup"
and for restore you can use:
pg_restore -i -h localhost -U "user" -d "dbname" -v "dbname.backup"
**ex:** pg_restore -i -h localhost -U thames -d thamesdb -v "thamesdb.backup"
to take backup of selected tabels(uses regular expressions) here
pg_dump -t '(A|B|C)'
for full details you can visit pgdump help page there are many options out there
If you want to take your backups hourly, I would think you should be using log archiving instead of pg_dump.

How to restore postgres database into another database name

I use the postgres today
and got a problem
I dump the database that way
pg_dump zeus_development -U test > zeus_development.dump.out
what if I wnat to restore to another database zeus_production
How could I do?
Simple, first create your database using template0 as your template database:
createdb -U test -T template0 zeus_production
Then, restore your dump on this database:
psql -U test zeus_production -f /path/to/zeus_development.dump.out
When restoring, always use template0 explicit, as it is always an empty and unmodifiable database. If you don't use an explicit template, PostgreSQL will assume template1, and if it has some objects, like a table or function that your dumped database already has, you will get some errors while restoring.
Nonetheless, even if you were restoring on a database with the same name (zeus_development) you should create (or recreate) it the same way. Unless you used -C option while dumping (or -C of pg_restore if using a binary dump), which I don't recommend, because will give you less flexibility (like restoring on a different database name).
The PostgresSQL documentation has influenced me to use the custom format. I've been using it for years and it seems to have various advantages but your mileage may vary. That said, here is what worked for me:
pg_restore --no-owner --dbname postgres --create ~/Desktop/pg_dump
psql --dbname postgres -c 'ALTER DATABASE foodog_production RENAME TO foodog_development'
There was no foodog_development nor foodog_production databases existing before the sequence.
This restores the database from the dump (~/Desktop/pg_dump) which will create it with the name it was dumped as. The rename names the DB to whatever you want.
The --no-owner may not be needed if your user name is the same on both machines. In my case, the dump was done as user1 and the restore done as user2. The new objects need to be owned by user2 and --no-owner achieves this.
Isn't it easier to simply do the following?
createdb -U test -T zeus_development zeus_production
This has an answer on dba.stackexchange, which I reproduce here:
Let's define a few variables to make the rest easier to copy/paste
old_db=my_old_database
new_db=new_database_name
db_dump_file=backups/my_old_database.dump
user=postgres
The following assumes that your backup was created with the "custom" format like this:
pg_dump -U $user -F custom $old_db > "$db_dump_file"
To restore $db_dump_file to a new database name $new_db :
dropdb -U $user --if-exists $new_db
createdb -U $user -T template0 $new_db
pg_restore -U $user -d $new_db "$db_dump_file"
Here's a hacky way of doing it, that only works if you can afford the space and time to use regular .sql format, and if you can safely sed out your database name and user.
$ pg_dump -U my_production_user -h localhost my_production > my_prod_dump.sql
$ sed -i 's/my_production_user/my_staging_user/g' my_prod_dump.sql
$ sed -i 's/my_production/my_staging/g' my_prod_dump.sql
$ mv my_prod_dump.sql my_staging_dump.sql
$ sudo su postgres -c psql
psql> drop database my_staging;
psql> create database my_staging owner my_staging_user;
psql> \c my_staging;
psql> \i my_staging_dump.sql
If your dump does not include the name, the restore will use the DB defined in DESTINATION. Both SOURCE and DESTINATION are Connection URLs.
Dump without --create
pg_dump \
--clean --if-exists \
--file ${dump_path} \
--format=directory \
--jobs 5 \
--no-acl \
--no-owner \
${SOURCE}
Restore without --create
pg_restore \
--clean --if-exists \
--dbname=${DESTINATION} \
--format=directory \
--jobs=5 \
--no-acl \
--no-owner \
$dump_path