简体   繁体   中英

Move data from a postgres database to another database with a different owner

I have two postgres databases with the same structure and tables, hosted on the same server, the databases are owned by different users:

database1 : owner1

database2 : owner2

I want to know the best way to copy the content of database1 into database2 (overriding the original content of database2 ).

I tried pg_dump & pg_restore but the dump will explicitly specify owner1 as the owner of the tables and then I have permission issues when trying to get the data from database2 using owner2 . I had to manually re-grant all privileges on database2 to owner2 and set the owner of all tables again to owner2 .

My approach:

pg_dump database1 > database1.psql
postgres=# drop database database2;
postgres=# create database database2;
psql --d database2  -f database1.psql

Is there a simpler way to copy the data from database1 to database2 without having to update the user permissions manually after the restore.

Yes, you can use pg_dump to specify that you don't want to export ownership:

  • -O (or --no-owner ) does not export ownership
  • -x (or --no-privileges ) Prevent dumping of access privileges (grant/revoke commands)
    pg_dump db_name -O -x > output_file

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