Postgres:创建后无法连接数据库?

时间:2015-03-12 10:52:06

标签: postgresql debian

我正在尝试在Debian 7上创建一个新的Postgres数据库和超级用户。

但是,一旦我创建了新数据库,我似乎无法连接到我的新数据库。

这就是我所做的:

$ sudo su - postgres
postgres@:~$ createdb prescribing
postgres@$ psql
postgres=# create user prescribing with password 'mypassword';
CREATE ROLE
postgres=#  GRANT ALL PRIVILEGES ON DATABASE prescribing to prescribing;
GRANT
postgres=# exit\q
could not save history to file "/var/lib/postgresql/.psql_history": No such file or directory
postgres@:~$ psql -d prescribing -U prescribing
psql: FATAL:  Peer authentication failed for user "prescribing"

我做错了什么?

我不确定错误是否与缺少历史文件有关,或者是否是Unix用户和数据库用户之间的一些我不完全理解的交互。

我也试过psql -U prescribing -d prescribing -W,看看我是否可以让它来问我密码,但它没有帮助。

更新:以下是pg_hba.conf的内容:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

1 个答案:

答案 0 :(得分:2)

使用'peer'身份验证,unix用户必须匹配数据库用户。您正尝试以“处方”用户身份进行连接,但以linux“postgres”用户身份登录。由于这种不匹配,它不起作用。

选择'peer'身份验证是因为您在本地连接(通过unix套接字),而pg_hba.conf指定使用'peer'。如果将“本地”条目更改为md5,则可以使用密码验证。或者,您可以通过TCP / IP地址127.0.0.1进行连接,例如:

psql -h 127.0.0.1 -U prescribing -d prescribing
相关问题