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

Ubuntu环境下Git克隆的Rails项目数据库创建/迁移失败求助

Hey Martin, let’s work through this step by step to get your Rails app connected to PostgreSQL and resolve those database setup errors.

1. Create the missing database.yml file

Since your cloned project doesn’t have this critical configuration file, we’ll build it from scratch in your config directory. This file tells Rails exactly how to connect to your PostgreSQL database.

Create the file with the following content (make sure the username/password match what you set up earlier):

# config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: localhost
  username: postgres_user  # Use the PostgreSQL user you created
  password: 'password'     # Use the password you set for that user

development:
  <<: *default
  database: make_dev       # Matches the database name from your error message

test:
  <<: *default
  database: make_test

production:
  <<: *default
  database: make_prod

2. Fix PostgreSQL Authentication Settings

The fe_sendauth: no password supplied error happens because PostgreSQL defaults to "peer" authentication (which uses your system user identity instead of a password) for local connections. We need to switch this to password-based authentication so Rails can connect properly.

Step 2.1: Locate and edit pg_hba.conf

Find your PostgreSQL config file (replace <version> with your installed PostgreSQL version, e.g., 12 on most Ubuntu setups):

sudo nano /etc/postgresql/<version>/main/pg_hba.conf

Look for these lines:

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            peer

Change peer to md5 for all three lines:

local   all             postgres                                md5
local   all             all                                     md5
host    all             all             127.0.0.1/32            md5

Save the file (Ctrl+O, press Enter) and exit nano (Ctrl+X).

Step 2.2: Restart PostgreSQL to apply changes

sudo systemctl restart postgresql

Step 2.3: Verify your PostgreSQL user can connect

Test if your user can log in with the password you set:

psql -U postgres_user -d make_dev -h localhost

If you’re prompted for a password and can access the PostgreSQL shell, the connection is working. Type \q to exit.

3. Run the Rails Database Commands Correctly

Always use bundle exec to ensure you’re using the gem versions specified in your project’s Gemfile.lock. Also, explicitly specify the environment to avoid confusion:

Create the database:

bundle exec rake db:create RAILS_ENV=development

Run migrations:

bundle exec rake db:migrate RAILS_ENV=development

4. Address Other Minor Issues

  • Devise.rb warning: The already initialized constant OpenSSL::SSL::VERIFY_PEER warning means your config/initializers/devise.rb has a line that duplicates a setting Rails already handles. Find that line in the file and comment it out (add a # at the start).
  • Recaptcha.rb: Keep the lines commented out for now—you can set up the reCAPTCHA API keys later once your database is working.
  • Bugsnag warning: This is just a notice that the Bugsnag API key isn’t set; it won’t block your database setup. You can configure it later if needed.

Quick Recap of Best Practices

  • Never run Rails commands as the root user—stick with your regular martin user to avoid permission conflicts.
  • Double-check that your PostgreSQL username/password in database.yml exactly matches what you created in the PostgreSQL shell.

Let me know if you hit any snags along the way!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:22:33