Postgres: copy one remote DB to another - postgresql

I'm trying to copy a database from one remote server to another one. I've tried several different commands from my terminal (macOS):
"pg_dump -U postgres -d [DB] -f [DB].sql"
"pg_dump -U postgres -d [DB] -h [Host] -f [DB].sql"
But nothing works. I get errors like "pg_dump: error: connection to database [DB] failed: FATAL: database [DB] does not exist".
Any ideas how to solve this problem? I've tried to edit the pg_hba.conf, but it didn't work as well..

The procedure to make this work is :
First sub-option below outputs plain text file, second custom format(binary) file
a) pg_dump -C -h host_name -U user_name -d database_name -f database.sql
-C tells pg_dump to provide the command to create the database
on restore.
b) pg_dump -Fc -h host_name -U user_name -d database_name-f database.out
To restore you need different programs
a) For plain text option 1a do:
psql -d postgres -h host_name -U user_name -f database.sql
You need to connect to existing database, postgres in this case,
and then the commands from -C above will create
the database(database_name) and then connect to it for rest of operation.
b) For custom format option 1b:
pg_restore -C -d postgres -h host_name -U user_name database.out
Note: just specify the dump file(database.out) do not use -f.
More options and details can be found:
pg_dump
and
pg_restore
Look in the Notes section at the bottom of link for examples.

Related

How to restore .bak file using pg_restore command through cmd in PostgreSQL?

I have a test.bak file created with pg_dump command which has a hypertable and i have created a new database Performance_Test in postgreSQL.
The database was dumped using the command:
pg_dump -h localhost -U postgres -p 5432 -Fc -f "D:\Database Backup\temp.bak" Performace_Test
I want to restore that test.bak file in Performance_Test.
How can i do that?
You can restore doing the below(with caveats noted further on):
pg_restore -h localhost -d Performace_Test -U postgres -p 5432 "D:\Database Backup\temp.bak"
The caveats are:
If Performace_Test was created as mixed case by quoting you will need to quote the name above e.g. "Performace_Test"
If Performace_Test is not empty then you will get a bunch of errors.
Also assumes that Performace_Test is on the same cluster as you specified in the pg_dump command.
psql Performance_Test < test.bak

Restoring DB for PostgreSQL [duplicate]

I take backup using
pg_dump db_production > postgres_db.dump
and then I copy it to localhost using scp.
Now when I import on my local db it gives an error
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
by using commad line
pg_restore -d db_development postgres_db.dump
From the pg_dump documentation:
Examples
To dump a database called mydb into a SQL-script file:
$ pg_dump mydb > db.sql
To reload such a script into a (freshly created) database named newdb:
$ psql -d newdb -f db.sql
To dump a database into a custom-format archive file:
$ pg_dump -Fc mydb > db.dump
To dump a database into a directory-format archive:
$ pg_dump -Fd mydb -f dumpdir
To reload an archive file into a (freshly created) database named newdb:
$ pg_restore -d newdb db.dump
From the pg_restore documentation:
Examples
Assume we have dumped a database called mydb into a custom-format dump file:
$ pg_dump -Fc mydb > db.dump
To drop the database and recreate it from the dump:
$ dropdb mydb
$ pg_restore -C -d postgres db.dump
The answer above didn't work for me, this worked:
psql db_development < postgres_db.dump
In order to create a backup using pg_dump that is compatible with pg_restore you must use the --format=custom / -Fc when creating your dump.
From the docs:
Output a custom-format archive suitable for input into pg_restore.
So your pg_dump command might look like:
pg_dump --file /tmp/db.dump --format=custom --host localhost --dbname my-source-database --username my-username --password
And your pg_restore command:
pg_restore --verbose --clean --no-acl --no-owner --host localhost --dbname my-destination-database /tmp/db.dump
For me when i try to restore from remote host i used
psql -U username -p 5432 -h 10.10.10.1 -d database < db.dump
worked fine. And if not remote just following command worked.
psql -d database < db.dump
For me, It's working like this one.
C:\Program Files\PostgreSQL\12\bin> psql -U postgres -p 5432 -d dummy -f C:\Users\Downloads\d2cm_test.sql
If you restore .SQL file.
Create a new database in pgAdmin.
Go to the terminal and navigate the folder/directory where your .sql file is located. And then write the following command in terminal.
Syntax:
supername user postgres psql newDatabasename < inputfile.sql
Examaple:
sudo -u postgres psql newDb < restoreDb.sql
I've got same error when tried to backup db with DBeaver. If anyone uses DBeaver interface instead of command line on Windows, make sure your selected format as tar during backup and restore settings.
If you have a full DB dump:
PGPASSWORD="your_pass" psql -h "your_host" -U "your_user" -d "your_database" -f backup.sql
If you have schemas kept separately, however, that won't work. Then you'll need to disable triggers for data insertion, akin to pg_restore --disable-triggers. You can then use this:
cat database_data_only.gzip | gunzip | PGPASSWORD="your_pass" psql -h "your_host" -U root "your_database" -c 'SET session_replication_role = replica;' -f /dev/stdin
On a side note, it is a very unfortunate downside of postgres, I think. The default way of creating a dump in pg_dump is incompatible with pg_restore. With some additional keys, however, it is. WTF?
if you use pg_dump with -Fp to backup in plain text format, use following command:
cat db.txt | psql dbname
to copy all data to your database with name dbname
psql -U <username> -d <database-name> -h <host-name> -f <backup.sql>
Providing a simple one line answer which worked for me and will work for you too for most cases
psql -U username -d database_name < dump_file.sql
If above gives role related errors then replace username with postgres.
psql -U postgres -d database_name < dump_file.sql
Probably when you create a backup you want to restore it in another network or create a remote restoration.
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
If you backup with this way, I think this will be more easy to import database.
pg_dump -h (remote db address) -a --column-inserts -U postgres (database name) > (file name).sql
For import,
psql
-f (file name).sql
--host (remote db address)
--port 5432
--username postgres
--password (your password)
--dbname (database you want to import)
I've been struggling with this as well. This is the combination of dump & restore commands that worked for me:
pg_dump -Ft -C -h database_host -U username database > DATA.dump
To restore
pg_restore -x --no-owner -d database DATA.dump
Remove the -x flag if you want to keep the same access privileges (ACLs) in your DB. You must have the same roles and users in the database for this.
https://www.postgresql.org/docs/15/app-pgdump.html
https://www.postgresql.org/docs/15/app-pgrestore.html
here is the solution,
pg_restore -U username -p 5432 -h 10.10.10.1 -d database_name < dump_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

How to create a backup of a single table in a postgres database?

Is there a way to create a backup of a single table within a database using postgres? And how? Does this also work with the pg_dump command?
Use --table to tell pg_dump what table it has to backup:
pg_dump --host localhost --port 5432 --username postgres --format plain --verbose --file "<abstract_file_path>" --table public.tablename dbname
If you are on Ubuntu,
Login to your postgres user sudo su postgres
pg_dump -d <database_name> -t <table_name> > file.sql
Make sure that you are executing the command where the postgres user have write permissions (Example: /tmp)
Edit
If you want to dump the .sql in another computer, you may need to consider skipping the owner information getting saved into the .sql file.
You can use pg_dump --no-owner -d <database_name> -t <table_name> > file.sql
pg_dump -h localhost -p 5432 -U postgres -d mydb -t my_table >
backup.sql
You can take the backup of a single table but I would suggest to take the backup of whole database and then restore whichever table you need. It is always good to have backup of whole database.
9 ways to use pg_dump
If you prefer a graphical user interface, you can use pgAdmin III (Linux/Windows/OS X). Simply right click on the table of your choice, then "backup". It will create a pg_dump command for you.
you can use this command
pg_dump --table=yourTable --data-only --column-inserts yourDataBase > file.sql
you should change yourTable, yourDataBase to your case
As an addition to Frank Heiken's answer, if you wish to use INSERT statements instead of copy from stdin, then you should specify the --inserts flag
pg_dump --host localhost --port 5432 --username postgres --format plain --verbose --file "<abstract_file_path>" --table public.tablename --inserts dbname
Notice that I left out the --ignore-version flag, because it is deprecated.
Use the following command to get the compressed version of the table dump :
pg_dump -h localhost -p 5432 -U <username> -d <dbname> -t <tablename> -Fc -f backup.out
Here is how I do it.
pg-dump -h localhost -U postgres -p 5432 -t table database > path/to/store/name.sql
and run it like this
psql -h localhost -U postgres -p 5432 database < path/to/store/name.sql

Restore a postgres backup file using the command line?

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