Tetramputechture
Tetramputechture

Reputation: 2921

Cannot connect to Amazon PostgreSQL RDS via EC2

I currently have a PostgreSQL database setup on Amazon RDS. The database is connected to an Elastic Beanstalk server that runs a Rails application, and the Rails app works fine with it (on the production site, I can modify the database just fine. I can also view the database over SQL workbench just fine, and I can see that the changes I have made on the production site reflect in the database).

So i know the database is working correctly, and that my elastic beanstalk server connects to it fine, and my Rails app connects to it fine too.

However, when I try to connect to the database via my EC2 instance (note: this instance is in a different region (Ohio) than my EB and RDS instance (N. Virginia), I get this error:

psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

The directory /var/run/postgresql exists, but that file does not exist. I tried searching for that file from root, and it turns out that it doesn't exist anywhere.

This is a problem because I need to be able to seed the database remotely, and trying to run rake db:seed from the console produces this error. The error also arises when I run the rails console in production, and try to view anything that is part of the database.

This is my config/database.ymlfile:

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
    <<: *default
    adapter: postgresql
    encoding: unicode
    database: <%= ENV['RDS_DB_NAME'] %>
    username: <%= ENV['RDS_USERNAME'] %>
    password: <%= ENV['RDS_PASSWORD'] %>
    host: <%= ENV['RDS_HOSTNAME'] %>
    port: <%= ENV['RDS_PORT'] %>

This is how EB knows to connect to the PG database upon deployment.

I have tried to edit the security rules for my RDS to accept PostgreSQL connections from the EC2's public IP, but that doesn't seem to work.

Can anyone help me with this?

Edit:

Ok, I can connect to the remote database using the psql command with options specifying the server just fine. I tested it by executing a simple query, and I am positive that the connection is established. I am now unsure how to tell the server to connect to it automatically, though.

Upvotes: 1

Views: 1321

Answers (2)

Tetramputechture
Tetramputechture

Reputation: 2921

I solved this issue by accessing my EB instance via

eb ssh

and going to my app directory located in

var/app/current

and running

rails db:seed

It correctly seeded the production database with my values in seeds.rb

Upvotes: 0

TJ Biddle
TJ Biddle

Reputation: 6484

It looks like your application is attempting to connect to PostgreSQL on the localhost; it seems like your config/database.yml file is correct in that it is looking for the environment variable RDS_HOSTNAME - so my best guess would be that you are not setting that and it is defaulting to your localhost as it is empty.

You'll need to set this environment variable, along with the other items stated there (Database, user, pass & port) in order for this to work.

This StackOverflow answer explains how to set environment variables for Elastic Beanstalk.

Upvotes: 1

Related Questions