Ruby on rails PostgreSQL:致命-用户的对等身份验证失败(PG::ConnectionBad)
我正在使用PostgreSQL,我有一个用户的密码与database.yml中指定的密码匹配Ruby on rails PostgreSQL:致命-用户的对等身份验证失败(PG::ConnectionBad),ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我正在使用PostgreSQL,我有一个用户的密码与database.yml中指定的密码匹配 postgres=# select * from pg_user ; usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig ------------+----------+-------------+----------+-----------+--
postgres=# select * from pg_user
;
usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig
------------+----------+-------------+----------+-----------+---------+----------+----------+-----------
goodsounds | 16386 | t | t | t | t | ******** | |
postgres | 10 | t | t | t | t | ******** | |
(2 rows)
但是当我尝试通过运行命令创建数据库时
rails db:create
我得到了错误
致命:用户“goodsounds”的对等身份验证失败
这是我的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 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust
以前,上面的“信任”是md5,但我改为看看这是否有帮助
这是我的database.yml:
# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
# gem install pg
# On Mac OS X with macports:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
development:
adapter: postgresql
encoding: unicode
database: goodsounds_development
pool: 5
username: goodsounds
password: test
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
host: localhost
port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# The server defaults to notice.
#min_messages: warning
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: unicode
database: goodsounds_test
pool: 5
username: goodsounds
password: test
production:
adapter: postgresql
encoding: unicode
database: goodsounds_production
pool: 5
username: goodsounds
password: test
“对等身份验证”意味着它使用unix套接字,并希望连接的unix用户具有与postgresql用户名相同的unix用户名
由于您的本地unix用户名是funkdified
,并且您正试图通过unix域套接字(local
)连接(您的pg_hba.conf
指定了peer
身份验证),因此pg正确地拒绝了您的连接尝试
这是使用unix套接字时许多安装的默认行为
你可以:
- 通过在数据库连接设置中指定主机名,通过TCP/IP进行连接李>
- 编辑
以使用pg_hba.conf
密码身份验证,而不是unix套接字(md5
连接类型)的local
身份验证,以便pg接受密码身份验证;或peer
- 使用与您的unix用户名相同的PostgreSQL用户名连接,如果用户尚不存在,则在PostgreSQL中创建该用户
pg_hba.conf
的更改不会立即生效,您必须重新启动或至少重新加载PostgreSQL才能使其重新读取pg_hba.conf
哦,另外,如果您安装了多个PostgreSQL版本,您可能会从一个版本安装一个libpq,从另一个版本安装一个服务器。在这种情况下,请确保libpq默认连接到的unix套接字的位置与服务器的
unix\u socket\u目录相同
,或者在连接字符串中使用(例如),host=/tmp
。如果“对等身份验证”不起作用,请尝试md5身份验证
要指定主机,请尝试以下操作:
psql -d <dbname> -U <username> -h <hostname>
psql-d-U-h
或者这个:
psql -d <dbname> -U <username> -h <hostname> -W
psql-d-U-h-W
我在Ubuntu机器上遇到了同样的问题,所以我通过以下步骤消除了这个错误。
切换到postgres用户
$ sudo su - postgres
它将询问密码,默认密码为postgres
将用户切换到postgres后,打开psql控制台
$ psql
因此,如果有多个版本可用,请检查postgres的版本
psql=# select VERSION();
PostgreSQL 9.1.13 on x86_64-unk.... # so version is 9.1
现在打开postgres用户
vim /etc/postgresql/9.1/main/pg_hba.conf
9.1
是从上级命令返回的版本
替换
local all postgres peer
到
重新启动服务
sudo service postgresql restart
我也在我的博客上写步骤
编辑/etc/postgresql/9.3/main/pg_hba.conf
# "local" is for Unix domain socket connections only
local all all peer
改为“下线”,它对我有效
# "local" is for Unix domain socket connections only
local all all md5
以下是我解决问题的方法 运行下面的命令以确认您的PostgreSQL版本
psql --version
导航到服务器上的PostgreSQL配置目录,该目录位于/etc/PostgreSQL/10/main
中。请注意,10
是我在服务器上安装的PostgreSQL版本。您的版本可能是9.5
、11
或12
或任何其他版本
cd ~
cd /etc/postgresql/10/main
导航到/etc/postgresql/10/main
目录后,使用下面的命令打开文件pg_hba.conf
。此文件控制:允许连接哪些主机,如何验证客户端,可以使用哪些PostgreSQL用户名,可以访问哪些数据库:
sudo nano pg_hba.conf
替换以下行:
# Database administrative login by Unix domain socket
local all postgres peer
# Database administrative login by Unix domain socket
local all postgres md5
# "local" is for Unix domain socket connections only
local all all peer
# "local" is for Unix domain socket connections only
local all all md5
用下面的一行:
# Database administrative login by Unix domain socket
local all postgres peer
# Database administrative login by Unix domain socket
local all postgres md5
# "local" is for Unix domain socket connections only
local all all peer
# "local" is for Unix domain socket connections only
local all all md5
此外,替换以下行:
# Database administrative login by Unix domain socket
local all postgres peer
# Database administrative login by Unix domain socket
local all postgres md5
# "local" is for Unix domain socket connections only
local all all peer
# "local" is for Unix domain socket connections only
local all all md5
用下面的一行:
# Database administrative login by Unix domain socket
local all postgres peer
# Database administrative login by Unix domain socket
local all postgres md5
# "local" is for Unix domain socket connections only
local all all peer
# "local" is for Unix domain socket connections only
local all all md5
此外,我们可能希望在生产环境中允许到PostgreSQL数据库的入站连接(允许使用md5方法从所有数据库、所有用户和所有地址进行主机连接)
要实现此目的,请在文件末尾添加以下行,然后保存文件:
# remote connections
host all all all md5
仍然在/etc/postgresql/10/main
目录中,使用以下命令打开并编辑文件postgresql.conf
:
sudo nano postgresql.conf
sudo systemctl restart postgresql # To restart
sudo systemctl reload postgresql # To reload
将行\listen\u address='127.0.0.1'
或行listen\u address='127.0.0.1'
或行\listen\u address='localhost'
或行listen\u address='localhost'
替换为以下行,以允许PostgreSQL数据库侦听来自所有地址的连接:
listen_addresses = '*'
保存文件,然后导航到服务器的根目录:
cd ~
使用以下命令重新启动或重新加载PostgreSQL server:
sudo nano postgresql.conf
sudo systemctl restart postgresql # To restart
sudo systemctl reload postgresql # To reload
“在数据库连接设置中指定主机名”是指“将主机:本地主机添加到database.yml”吗?因为那对我不起作用。@Maarrensp是的。如果它对您“不起作用”,请发布一个带有详细信息的新问题(版本、pg_hba.conf和database.yml内容、确切的错误消息等)。将内容链接回此链接,并在此处添加带有新问题链接的注释。当我将“主机:'localhost'”添加到连接设置时,它解决了我的问题。谢谢大家!
sudo-u postgres psql dbname
将是对等方法的一种方式,其中postgres
是相关用户。第三点很容易被忽略,但很重要。当我创建了一个与我的Linux用户名匹配的Postgres用户名,然后在rails应用程序中设置它时,我就解决了这个问题。我当时不必为用户名或密码添加条目。该问题与psql
无关,因此指定psql
命令行选项不是回答该问题的有用答案。这是在问为什么rake db:create
不能与看似合理配置的Rails应用程序一起工作。这个问题与psql
无关,因此指定psql
co