简体   繁体   中英

Restore database from pg_dump

I had non-empty db and made backup it by:

pg_dump myBase > backup.sql

Then I must to delete db and now I am trying to restore it from backup.sql. So first I create database by:

create database myBase

grant privileges from psql

grant all privileges on database "myBase" to myUser

and then trying to recover from pg_dump by:

pg_dump myBase < backup.sql

And I didn't get any errors, but when I check tables by \\dt it's totally empty. I have also tried restoring with:

psql -h localhost -d myBase-U myUser-f backup.sql (no error, but no tables too)

psql myBase < backup.sql (like above)

I have a little expieriences with dbs so I assume it should be something obvious for more experienced user.

Can't reproduce the issue:

postgres=# create database so;
CREATE DATABASE
postgres=# \c so
You are now connected to database "so" as user "postgres".
so=# create table so(i int);
CREATE TABLE
so=# insert into so values (1);
INSERT 0 1
so=# \! pg_dump so > /tmp/so.sql
so=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# drop database so;
DROP DATABASE
postgres=# create database so;
CREATE DATABASE
postgres=# \c so
You are now connected to database "so" as user "postgres".
so=# \dt
No relations found.
so=# \! psql so -f /tmp/so.sql
SET
SET
SET
SET
SET
SET
CREATE EXTENSION
COMMENT
SET
SET
SET
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
so=# \dt
        List of relations
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | so   | table | postgres
(1 row)

Got the same problem, found the solution here: Unable to restore psql database from pg_dump with a different username

pg_dump dbname -O -x > backupname.sql

-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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM