最近,我使用此命令对我的postgres数据库进行了备份
pg_dumpall > BkpOldPG.sql
Run Code Online (Sandbox Code Playgroud)
删除旧版本的postgres后,我下载了最新版本9.4,并尝试使用以下命令恢复旧数据:
mody@debian:~$ su postgres
Password:
postgres@debian:/home/mody$
postgres@debian:/home/mody$ /usr/lib/postgresql/9.4/bin/psql -d postgres -f Documents/Bkp01dPg.sql
Documents/Bkp01dPg.sql: Permission denied
Run Code Online (Sandbox Code Playgroud)
如您所见,我被拒绝了权限,所以我尝试使用sudo,但是它不起作用:
postgres@debian:/home/mody$ sudo /usr/lib/postgresql/9.4/bin/psql -d postgres -f Documents/Bkp01dPg.sql
[sudo] password for postgres:
postgres is not in the sudoers file. This incident will be reported.
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?
谢谢!
您的备份文件或其所在的Documents文件夹具有不允许postgres用户访问的权限。
您可以授予postgres用户(和系统上的所有其他用户)阅读权限:
chmod a+x Documents
chmod a+r Documents/Bkp01dPg.sql
Run Code Online (Sandbox Code Playgroud)
或者,您可以复制Bkp01dPg.sql到postgres用户已经可以访问的位置,然后将其postgres所有权授予用户,例如
sudo cp Documents/Bkp01dPg.sql ~postgres/
sudo chown postgres ~postgres/Bkp01dPg.sql
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用普通用户帐户运行还原,并以超级用户身份连接到PostgreSQL:
psql -U postgres -f Documents/Bkp01dPg.sql
Run Code Online (Sandbox Code Playgroud)
...,尽管您可能需要修改pg_hba.conf或pg_ident.conf允许用户连接,postgres就像您这样做一样。(或者,您可以临时ALTER USER授予普通用户超级用户权限)。
顺便说一句,你不需要su来postgres。养成习惯于使用sudo -u postgres运行命令以及sudo -u postgres -i是否需要交互式命令行的习惯。