如何正确使用pg_dump将本地数据库复制到远程服务器

问题描述

我正在努力尝试将本地的postgres数据库移至我的Digital Ocean集群。我想将新的远程服务器设为我的主dB,因此可以通过所有角色和特权。我阅读了pg_dump文档,但仍然不知道如何解决我的问题。这些是我下面的命令:

创建转储文件

pg_dump -U postgres --no-owner sb > dump_file  

从DO连接到远程服务器并移动dB:

pg_restore -d {MY_URI_CONNECTION} dump_file

创建了数据,架构和表,但没有创建角色,这在我的终端中引发了错误

ERROR:  role "admin" does not exist
ERROR:  role "admin" does not exist
ERROR:  role "web_anon" does not exist
WARNING:  no privileges Could be revoked for "public"
REVOKE
WARNING:  no privileges were granted for "public"
GRANT
ERROR:  role "web_anon" does not exist
ERROR:  role "web_anon" does not exist
ERROR:  role "web_anon" does not exist
ERROR:  role "web_anon" does not exist
ERROR:  role "web_anon" does not exist
ERROR:  must be member of role "postgres"
ERROR:  role "USER" does not exist

我尝试了多种方法来迁移我的dB,但是我一直遇到相同的错误。我该如何迁移我所有的dB,包括角色和权限,以克服上面的错误

解决方法

使用pg_dumpall -g > globals_file.sql。参见此处pg_dumpall。这将为您提供所有全局变量(包括角色)的纯文本文件。首先使用psql -d db -U superuser -f globals_file.sql恢复到新群集。请注意,这将转储给定群集中的所有角色。