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

PostgreSQL连接错误:no pg_hba.conf entry问题求助(Rails新手)

Fixing PG::ConnectionBad Error When Switching to PostgreSQL in Rails

Hey there! Let's work through this PostgreSQL connection error together—it's a super common hiccup when moving from SQLite to Postgres locally, so you're definitely not alone. Here's a step-by-step breakdown to get your Rails app connected properly:

Step 1: Locate your pg_hba.conf file

This file controls PostgreSQL's host-based authentication rules, and the error means there's no rule allowing your user/host/database combination to connect. To find where it lives:

  • Open your terminal and run: psql -U postgres -c 'SHOW hba_file;'
    (You might need to enter the postgres superuser password if you set one up)
  • Common paths by system:
    • Mac (Homebrew): /usr/local/var/postgres/pg_hba.conf
    • Ubuntu/Debian: /etc/postgresql/<your-postgres-version>/main/pg_hba.conf
    • Windows: C:\Program Files\PostgreSQL\<version>\data\pg_hba.conf

Step 2: Update the authentication rules in pg_hba.conf

Open the file with a text editor (you'll need sudo/admin rights on most systems). Look for lines starting with local or host and adjust them to allow your connection:

  • For local Unix socket connections (no host specified in database.yml):
    Change this line:

    local all all peer
    To:
    local all all trust
    Or if you prefer password authentication:
    local all all md5

  • For TCP/IP connections (if you set host: localhost in database.yml):
    Make sure these lines exist (add them if not):

    host all all 127.0.0.1/32 md5
    host all all ::1/128 md5

Step 3: Restart PostgreSQL to apply changes

The new rules won't take effect until you restart the service:

  • Mac (Homebrew): brew services restart postgresql
  • Ubuntu/Debian: sudo systemctl restart postgresql
  • Windows: Open Services (search "Services" in Start menu), find PostgreSQL, right-click and select "Restart"

Step 4: Verify your Rails database.yml configuration

Double-check your config/database.yml to make sure it matches your Postgres setup. A typical development config should look like this:

development:
  adapter: postgresql
  database: tuma_database_development
  username: keemtaker
  # Add a password line if you set one for the keemtaker user
  # password: your_password_here
  host: localhost # Optional—omit to use local sockets
  port: 5432

Step 5: Ensure your database and user exist

If you haven't already, create the Postgres user and database for your app:

  • Create the user (with superuser privileges for local development): createuser keemtaker -s
  • Create the database: createdb tuma_database_development

After following these steps, try running rails db:migrate again or starting your Rails server—this should resolve the connection error. If you still run into issues, double-check that Postgres is running and that your database.yml has no typos in the username/database name.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:04:22