我需要从computer1中以postgres用户名1转储一个postgres数据库,然后在具有postgres用户名2的computer2上恢复它。我一直遇到似乎备份文件要使用username1的错误:
当我在computer2上运行它时:
psql dbname < backupname.pgsql
我收到此错误:
ERROR: role "username1" does not exist
我尝试过:
// Dumping from computer1:
pg_dump dbname > backupname.sql
pg_dump dbname > backupname.pgsql
pg_dump -U username1 dbname -N topology -T spacial_ref_sys > backupname.pgsql
// Restoring on computer2:
psql dbname < backupname.pgsql
是否需要对转储或还原进行修改以克服此问题?
答案 0 :(得分:2)
问题出在转储上。借助this post的见解,我可以使用以下方法解决此问题:
// On Computer1
pg_dump dbname -O -x > backupname.sql
// On Computer2
psql dbname < backupname.sql
与pg_dump
一起使用的选项标志是:
-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)
有关选项标志的更多信息,请参见PostgreSQL docs for pg_dump。
答案 1 :(得分:1)
您不需要通过丢弃所有者/特权来破坏转储。您可以在还原时进行。
将select name, day,
sum(value) over (partition by name order by day) total
from tablename
与pg_restore
(可能还有--no-acl
)选项一起使用:
-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.
类似这样:
--no-owner
答案 2 :(得分:0)
如果您使用的是pgAdmin,则可以使用Owner删除DumpOptions#2中的复选框,否则可以删除特权,例如
--no-privileges
,并在转储查询(如
)中删除所有权(如--no-password
)
/usr/bin/pg_dump --host localhost --port 5432 --username "postgres" --no-password --format custom --no-privileges --no-tablespaces --verbose --file "as" "databasename"
。
另外,如果您对表有约束,那么在创建转储时也要禁用触发器。
如果无法创建数据库的另一个备份,则另一种方法是将转储数据库的所有者和角色复制到新数据库。如果您不这样做,则会收到一条错误消息,说 “ ACL不存在”(不确定是否很久以前存在)