Unable to restore psql database from pg_dump with a different username

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 18:38:30

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.

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)

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!