Search code examples
ruby-on-railspostgresqlamazon-web-servicesamazon-ec2amazon-rds

AWS ElasticBeanstalk EC2 gives postgresql connection error


When I deploy my app on aws beanstalk using eb deploy it gives an error

rake aborted!
PG::ConnectionBad: 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"?

.ebextensions/postgres.config:

packages:
yum:
 postgresql93-devel: []

config/database.yml:

default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5

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

Solution

  • I had the exact same issue.

    The problem is that you have to connect your database to the application through the environment variables.

    First you need to create an RDS instance to connect to. This link shows you how: https://aws.amazon.com/getting-started/tutorials/create-connect-postgresql-db/?nc1=h_ls

    Then, after the instance is created, you need to go to the RDS Service page, and choose instance in the left menu. Choose the instance you created in the list and proceed to the section called "Connect". Those are the values you put inside those environment variables, with the HOSTNAME value being the endpoint.

    Hope it helped, worked for me!