postgresql - 如何以root身份将数据库导入PostgreSQL?

标签 postgresql

我想将我的数据库导入到我服务器的数据库中。因此,我将数据库转储文件复制到服务器的根目录并登录并执行以下操作:

root@iWidgetServer1:~# sudo -u postgres psql -U iwidget -d iwidget -f iwidget_dump2.sql 
could not change directory to "/root"
psql: FATAL:  Peer authentication failed for user "iwidget"

然而,iwidget 是一个角色,并已授予此数据库的所有权限:

root@iWidgetServer1:~# sudo -u postgres psql -l
could not change directory to "/root"
                                     List of databases
       Name       |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
------------------+----------+----------+-------------+-------------+-----------------------
 iwidget          | iwidget  | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =Tc/iwidget          +
                  |          |          |             |             | iwidget=CTc/iwidget
 postgres         | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
 sample_db        | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
 template0        | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
                  |          |          |             |             | postgres=CTc/postgres
 template1        | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
                  |          |          |             |             | postgres=CTc/postgres
 template_postgis | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
(6 rows)

我做错了什么?

最佳答案

来自文档:

The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.

您正在执行 sudo -u postgres,但正在尝试连接为 iwidget

您需要创建一个名为iwidget 的用户并以此用户身份登录。

关于postgresql - 如何以root身份将数据库导入PostgreSQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24424399/

相关文章:

sql - 将返回随机记录的 PostgreSQL 过程

PostgreSQL:如何获取以前的插入时间戳

nhibernate - Fluent NHibernate 中的表和列命名

sql - PostgreSQL 9.3 : Union two columns in one

sql - 在 postgres 中更改表太慢

python - 如何使用 SQLAlchemy 选择 PostgreSQL 数组中某个位置的值?

postgresql - 如何使用custom_variable_class

json - 无法从标量中提取元素 - postgresql 错误

node.js - Nodejs/PostgreSQL sequelize - 如何更新/更改数据库中的模型?

将参数传递给 PostgreSQL 查询时出现 Python TypeError : not all arguments converted during string formatting,