You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

克隆Rails应用本地运行时PostgreSQL无密码连接错误求解

问题

从GitHub克隆Rails应用后,执行bundle、yarn命令,运行rails db:create时出现错误:

connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied

对应的config/database.yml配置如下:

# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS 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'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: tourmind
  password: <%= ENV['TOURMIND_DATABASE_PASSWORD'] %>

development:
  <<: *default
  database: tourmind_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 running Rails.
  username: tourmind

  # The password associated with the postgres role (username).
  password: <%= ENV['TOURMIND_DATABASE_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: tourmind_test

# As with config/credentials.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 or a full connection URL as an environment
# variable when you boot the app. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
#   production:
#     url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
  <<: *default
  database: tourmind_production
  username: tourmind
  password: <%= ENV['TOURMIND_DATABASE_PASSWORD'] %>

不确定是否需要配置.env文件或其他操作,求解决方法。

解决方法

1. 配置环境变量(推荐方式)

你的database.yml依赖ENV['TOURMIND_DATABASE_PASSWORD']读取环境变量,按以下步骤设置:

  • 在项目根目录创建.env文件,添加内容:
    TOURMIND_DATABASE_PASSWORD=你的PostgreSQL用户tourmind的密码
    
  • 检查Gemfile是否包含dotenv-rails,没有的话执行bundle add dotenv-rails,之后重启终端。
  • 运行命令时确保环境变量加载:bundle exec dotenv rails db:create。

2. 确认PostgreSQL用户配置

先确保本地PostgreSQL存在tourmind用户并拥有正确权限:

  • 进入PostgreSQL命令行:psql -U postgres
  • 若用户不存在则创建:CREATE USER tourmind WITH PASSWORD '你的密码';
  • 赋予用户创建数据库的权限:ALTER USER tourmind CREATEDB;
  • 退出命令行:\q

3. 临时直接指定密码(仅本地测试用)

如果只是本地开发测试,可暂时在database.yml中直接填写密码(注意不要提交到版本控制):
修改development段的password行:

password: 你的PostgreSQL用户tourmind的密码

之后运行rails db:create即可。

4. 检查PostgreSQL认证方式

若以上方法无效,检查PostgreSQL的pg_hba.conf文件(macOS Homebrew路径通常为/usr/local/var/postgres/pg_hba.conf,Linux路径为/var/lib/postgresql/版本/main/pg_hba.conf):

  • 找到host all all 127.0.0.1/32的行,将认证方式改为md5(需要密码)或trust(无需密码,仅本地开发使用)。
  • 修改后重启PostgreSQL服务:macOS执行brew services restart postgresql,Linux执行sudo systemctl restart postgresql。

内容的提问来源于stack exchange,提问作者swaeetpotato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 16:05:42