从GitHub上传Ruby on Rails项目至AWS EC2实例时遇SQL错误求助
Hey there! I’ve helped tons of new Rails developers work through exactly this kind of deployment snag to AWS EC2, so let’s get your app up and running smoothly. The SQL errors you’re seeing almost always boil down to missing database setup steps or environment mismatches between your local machine and the EC2 instance—let’s break this down step by step:
1. Understand the Local vs. Production Database Difference
When you built your app locally, Rails probably used SQLite (the default database for development) and quietly handled setup for you—like creating the database file and running migrations when you started the server. But on EC2, production environments don’t work that way:
- If you stick with SQLite, the EC2 instance’s file permissions might block Rails from creating/writing to the database file.
- SQLite is not meant for production anyway—you’ll want to use PostgreSQL or MySQL for stability, which requires explicit setup.
2. Check & Fix Database Configuration
First, log into your EC2 instance and navigate to your Rails app folder. Let’s start with the simplest fix if you’re still using SQLite, then cover the recommended production setup.
Option A: Stick with SQLite (For Testing Only)
- Fix folder permissions so Rails can access the
dbdirectory:chmod -R 755 /path/to/your/rails/app/db - Manually create the production database:
RAILS_ENV=production rails db:create - Run your database migrations to create tables:
RAILS_ENV=production rails db:migrate
Option B: Switch to PostgreSQL (Recommended for Production)
SQLite isn’t built for production traffic, so let’s set up PostgreSQL properly:
- Install PostgreSQL on your EC2 instance:
sudo apt update && sudo apt install postgresql postgresql-contrib -y - Create a PostgreSQL user and database for your app:
# Create a superuser (matches your Rails app's default setup) sudo -u postgres createuser -s your_app_username # Create the production database owned by that user sudo -u postgres createdb your_app_name_production -O your_app_username - Update your
config/database.ymlfile to use PostgreSQL for production:
Open the file withnano config/database.ymland replace theproductionsection with:production: adapter: postgresql database: your_app_name_production username: your_app_username host: localhost pool: 5 timeout: 5000 - Run database setup commands again:
RAILS_ENV=production rails db:create RAILS_ENV=production rails db:migrate
3. Set Critical Production Environment Variables
Rails needs a few key environment variables to run in production:
- Generate a secret key for encryption (required for Rails to start):
rails secret - Set the secret key as an environment variable (temporarily; to make it permanent, add it to
~/.bashrcor your server’s environment config):export SECRET_KEY_BASE=your_generated_secret_key_here - Make sure you’re always running commands in production mode by setting:
export RAILS_ENV=production
4. Start the Production Server Correctly
Don’t use rails server for production—it’s meant for development. Instead, use Puma (Rails’ default production server):
bundle exec puma -d -p 3000
The -d runs it in the background, and -p 3000 sets the port to 3000 (make sure your EC2 security group allows inbound traffic on port 3000!).
5. Dig Into Logs If You Still Have Issues
If errors persist, check the production log for exact details—this will tell you exactly what’s wrong (missing table, permission error, etc.):
tail -f /path/to/your/rails/app/log/production.log
Look for lines starting with ERROR or FATAL—they’ll point you straight to the problem.
内容的提问来源于stack exchange,提问作者Evan Christensen

