postgresql - Postgres 恢复丢失的权限

标签 postgresql pgadmin pg-dump pg-restore

我正在运行一些非常基本的测试,以确认备份/恢复过程在本地环境中正常工作。

我遇到的问题是它看起来不像 pg_dump/pg_restore/psql 正在将数据库恢复到相同状态。

下面是我从头到尾所做的示例。

CREATE DATABASE testdb WITH ENCODING='UTF8' CONNECTION LIMIT=-1;
CREATE TABLE a
(
    a INT
);

INSERT INTO a(a)
SELECT 1 UNION ALL
SELECT 2;

SELECT * FROM a;

GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO testuser;

然后运行 ​​pg_dump

pg_dump -Fc -v --host=localhost --username=postgres --dbname=testdb -f C:\test\testdb.dump

为此示例创建并行还原

CREATE DATABASE testdb_restore WITH ENCODING='UTF8' CONNECTION LIMIT=-1;
pg_restore -v --host=localhost --username=postgres --dbname=testdb_restore C:\test\testdb.dump

现在,当我在 pgadmin 中右键单击 testdb 并单击“创建脚本”时,我得到以下内容

-- Database: testdb

-- DROP DATABASE testdb;

CREATE DATABASE testdb
    WITH 
    OWNER = postgres
    ENCODING = 'UTF8'
    LC_COLLATE = 'English_Australia.1252'
    LC_CTYPE = 'English_Australia.1252'
    TABLESPACE = pg_default
    CONNECTION LIMIT = -1;

GRANT ALL ON DATABASE testdb TO postgres;

GRANT TEMPORARY, CONNECT ON DATABASE testdb TO PUBLIC;

GRANT ALL ON DATABASE testdb TO testuser;

当我点击 perform the same on testdb_restore 时,我得到以下信息

-- Database: testdb_restore

-- DROP DATABASE testdb_restore;

CREATE DATABASE testdb_restore
    WITH 
    OWNER = postgres
    ENCODING = 'UTF8'
    LC_COLLATE = 'English_Australia.1252'
    LC_CTYPE = 'English_Australia.1252'
    TABLESPACE = pg_default
    CONNECTION LIMIT = -1;

如您所见,我缺少原始数据库的额外权限。

我确信这是一件非常简单的事情,但我目前对此一头雾水。我也尝试过使用方法,并且还添加了 pg_dump 创建数据库选项,但没有区别。

请注意:我对 postgres 非常陌生,并且有 SQL Server 背景。

最佳答案

角色和权限是按集群而不是按数据库存储和管理的,这就是 pg_dump 不转储它们的原因。如果您愿意转储整个集群,则应该使用 pg_dumpall

或者,您可以使用 pg_dumpall -r 仅转储角色,然后使用 pg_dump 您的数据库,并应用这两个脚本。

关于postgresql - Postgres 恢复丢失的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58407474/

相关文章:

postgresql - 重命名 postgres 表会删除现有索引吗?

mongodb - postgresql 或 Mongodb 用于存储位置数据(纬度和经度)并基于以下内容进行搜索?

postgresql - Postgres : pg_restore/pg_dump everything EXCEPT the table id's for a table

database - Postgres : Best way to move data from public schema of one DB to new schema of another DB

postgresql - Postgres - 如何将输出返回到终端?

c# - Postgresql:Double.MinValue 超出 double 类型的范围

postgresql - pgAdmin III 为什么查询结果被缩短了?

postgresql - pgAdmin 看不到 pgAgent 作业调度程序

postgresql - 从 pgadmin 获取的备份小于从 pg_dump 获取的备份

c# - 当输出文件名中包含 UTF-8 字符时 pg_dump 失败