我正在使用PostgreSQL,我有一个用户的密码与数据库中指定的密码匹配.yml

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)

但是当我try 通过运行命令创建数据库时

rails db:create

我明白了

致命:用户"goodsounds"的对等身份验证失败

这是我的pg_hba.形态:

# 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,但我改变了主意,看看这是否有帮助.

这是我的数据库.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)连接以用户goodsounds的身份进行连接,其中您的pg_hba.conf指定了peer身份验证,因此Pg正确地拒绝了您的连接try .

这是许多安装在使用unix套接字时的默认行为.

你可以:

  • 通过在数据库连接设置中指定主机名,通过TCP/IP进行连接;
  • 编辑pg_hba.conf以使用md5密码身份验证,而不是unix套接字(local连接类型)的peer身份验证,因此Pg接受密码身份验证;或
  • 使用与unix用户名相同的PostgreSQL用户名进行连接,如果用户还不存在,则在PostgreSQL中创建该用户.

参见the docs for pg_hba.confclient authentication chapter of the documentation的其余部分.

请注意,对pg_hba.conf的更改不会立即生效,必须重新启动或至少重新加载PostgreSQL才能使其重新读取pg_hba.conf.


哦,还有,如果你安装了多个PostgreSQL版本,你可能会有一个版本的libpq和另一个版本的服务器.在这种情况下,请确保libpq默认连接到的unix套接字的位置与服务器的unix_socket_directories相同,或者在连接字符串中使用(例如)host=/tmp覆盖它.

Postgresql相关问答推荐

转换失败:(—122.763091,49.04676)转换为地理(位置)""

读取扩展坞合成中的UDP:IO/超时

如何在PostgreSQL中对深度嵌套的jsonb属性创建索引

Postgres将唯一索引添加到已在分区上具有PK的分区表中,然后添加PK

将列类型从文本[]更改为jsonb[]

在Postgres游标中从一行变量中减go 另一行变量

右连接 postgresql 出现语法错误

当我写 SELECT ~1;在 Postgresql 上它给了我 -2 结果.这是什么原因?它一直持续〜4和-5等

安装 pg gem 失败,mkmf.rb 找不到 ruby​​ 的头文件(Mac OSX 10.6.5)

Postgres中的GROUP BY - JSON数据类型不相等?

Windows PSQL 命令行: is there a way to allow for passwordless login?

在 PostgreSQL 中Explain与explain analyze

mysql_insert_id 替代 postgresql

在 Spring Boot 应用程序中禁用表重新创建

从 PostgreSQL 序列中 Select 多个 id

当成功有时会导致退出代码为 1 时,如何可靠地确定 pg_restore 是否成功?

PostgreSQL 中的行编号

安装了 Postgres.app 但它不起作用

PostgreSQL 使用 UUID 与文本作为主键

当从 Heroku pg:pull 数据库时提示:role "root" does not exist.