PG::ConnectionBad:致命:用户密码验证失败

PG::ConnectionBad: FATAL: password authentication failed for user

我花了半天时间试图解决这个问题。我阅读了很多关于堆栈溢出和其他资源的主题,但没有找到任何有用的答案。

首先,我使用 PostgreSQL 标志创建了我的应用程序,并且关于 this tutorial,正在尝试设置我的环境:

这是我的 database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: localhost
  username: viter
  password: ******

development:
  <<: *default
  database: my_app_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: my_app

  # The password associated with the postgres role (username).
  #password:

  # 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

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #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
  # Defaults to warning.
  #min_messages: notice

# 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:
  <<: *default
  database: my_app_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:

但是在命令 rake db:create 之后我收到:

FATAL:  password authentication failed for user "viter"
FATAL:  password authentication failed for user "viter"
Couldn't create 'my_app_development' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL:  password authentication failed for user "viter"
FATAL:  password authentication failed for user "viter"

也尝试使用 但在 运行 'psql' 命令后收到此错误:

psql: FATAL: database "viter" does not exist

还有我的 pg_hba.conf 文件:

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

# 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
# Allow replication connections from localhost, by a user with the
# replication privilege.

确保您创建的用户 viter 和数据库 my_app_development 具有正确的权限。 从您的错误消息来看,您似乎尝试了一些不需要的数据库 viter

您面临的问题是您还没有为您的应用程序的数据库创建用户。

首先,您需要通过 postgres 创建 "viter" 用户; sudo -u postgres createuser viter -s

这也使 "viter" 成为超级用户。

然后,您需要在 psql 中使用此命令(作为 postgres 用户)为 "viter" 设置密码以匹配 "database.yml" 中使用的密码:\yourpassword viter

接下来,以 "viter" 作为所有者创建每个数据库(同时仍在 psql 终端中)。

用这个创建你的开发数据库:CREATE DATABASE my_app_development OWNER viter;

根据每个数据库的名称对 productiontest 数据库重复此操作。

例如; my_app_productionmy_app_test.

在你的config/database.yml中得到Postgres.app运行重写:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

这解决了我的问题