代码:
# 1. Cleanup, in case we run this script more than once
psql -U postgres -c 'DROP DATABASE IF EXISTS t1'
rm -rf t1_dump
# 2. Create database and grant all to janbet
psql -U postgres -c 'CREATE DATABASE t1'
psql -d t1 -U postgres -c 'GRANT ALL ON DATABASE t1 TO janbet'
# 3. Check access to schema public (it works)
psql -d t1 -U janbet -c 'CREATE TABLE public.x AS SELECT 1'
# 4. Create pg_dump
pg_dump -d t1 -U janbet --format=d --file=t1_dump
# 5. Restore database t1
pg_restore -d t1 -U postgres --clean t1_dump
# 6. Check access to schema public
psql -d t1 -U janbet -c 'CREATE TABLE public.y AS SELECT 1'
输出:
DROP DATABASE
CREATE DATABASE
GRANT
SELECT 1
ERROR: permission denied for schema public
在psql中,\ dn +在还原之前返回此值:
List of schemas
Name | Owner | Access privileges | Description
--------+----------+----------------------+------------------------
public | postgres | postgres=UC/postgres+| standard public schema
| | =UC/postgres |
还原后的结果:
List of schemas
Name | Owner | Access privileges | Description
--------+----------+-------------------+------------------------
public | postgres | | standard public schema
因此,显然缺少某些特权。问题:
GRANT USAGE ON SCHEMA public TO janbet
效果很好,但这也许不是必须的吗?在谷歌上搜索时发现doc,但那里没有我的问题的明确答案。
版本:
version
-----------------------------------------------------------------------------------------------------------------------------
PostgreSQL 10.4 (Ubuntu 10.4-0ubuntu0.18.04) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0, 64-bit