Cannot connect docker container on ECS Fargate to RDS db instance - docker

I've been pulling my hair out trying to connect my docker container on ECS Fargate to my RDS instance without success for the past 3 hours and I don't know why it's not working.
I have a ruby docker image with db config like this:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
<<: *default
host: <%= ENV['DATABASE_URL'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
database: my_project_db
which is the RDS endpoint URL.
My database identifier name is myprojectdbinstance and initial database name is my_project_db.
When I run my ECS task I get an error:
ActiveRecord::NoDatabaseError: We could not find your database: Which can be found in the database configuration file located at config/database.yml
PG::ConnectionBad: could not translate host name "" to address: Name does not resolve
My ECS and RDS db are in the same VPC. The ECS Task is in projects-alb-sg security group, my RDS accepts inbound traffic from projects-alb-sg security group. My VPC supports DNS resolution and DNS hostnames and RDS database is publicly available. I have created DB subnet group with the same subnets where my ECS task is located. I have already tried all possible settings and it doesn't work.
What am I missing?
My VPC has DNS resolution enabled
I span up an EC2 instance and did nslookup and here's the output
Non-authoritative answer: canonical name =
DB security group (it allows access from sg-047f1acd163d6f380)
sg-047f1acd163d6f380 security group that I set to my ECS task


Connect AWS Red Shift with Rails Project

I want to connect my rails(6) project with AWS RedShift.
I have tried these DOC1, DOC2 but getting the same error.
Error is "PG::ConnectionBad: could not connect to server: Connection timed out.
Is the server running on host "" (IP) and accepting TCP/IP connections on port 5439?"
Database configuration:
adapter: redshift
host: ''
port: 5439
database: 'DB Name'
username: 'Root User Name'
password: 'DB Password'
encoding: utf8
pool: 3
timeout: 5000
Red Shift cluster Security group inbound rules:
Type RedShfit
Protocol TCP
Port 5439
If your local computer on the Internet wants to connect to Redshift, then the following will need to be configured:
Redshift cluster is in a Public Subnet
Redshift cluster is configured as Publicly Accessible = Yes
The Security Group associated with the Redshift cluster has a rule that permits Inbound access on port 5439 from your own public IP address

Rails not able to access Postgresql deployed by Docker

I have a docker container that spins up my Postgres DB, Elasticsearch, and Redis; however, my rails server is unable to connect to the Postgres DB... The server starts, but when it attempts to connect to the DB, I get the error
PG::ConnectionBad (could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
When Docker launches the DB, it does so on localhost:5432. Via the terminal, I'm able to connect to the DB by running psql -h localhost -U myUser, but running psql results in the same error...
My rails database.yml file is configured to connect to the database at localhost:5432 with myUser, but I cannot figure out why it is giving me this error. It seems like Rails is attempting to connect to my server instead of the server Docker is running.
Here is my database.yml file
default: &default
adapter: postgis // I have tried "postgresql" as well
encoding: unicode
reconnect: true
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV['DATABASE_URL'] %>
<<: *default
database: my_db
username: <my username>
password: <my password>
Here is my .env
My docker container shows the following
DATABASE_URL: postgres://<my username>:<my password>#novus-postgis-localdev:5432
5432/tcp: :::5432
Therefore, my Rails environment is supposed to connect to my docker DB server, but it appears to be attempting to connect to my server (which isn't running).
What am I missing here / what is wrong with my configurations that have Rails looking at the wrong server?
FWIW, I'm on macOS and I installed Postgres via the
When the is running a server, the psql command connects to it.
When the Docker container is running, I am unable to start the Postgres. App server because port 5432 is already used (as expected)
I did find out that my Docker DB container is listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" whereas my Rails is pointing to the socket /tmp/.s.PGSQL.5432. I am unable to figure out how to change either of these to be the same (rails to look at /var/run/postgresql/... or docker to launch socket at /tmp/...
In the Postgresql configuration file, try and enter,

Deploying a Rails 5 app to EC2- RDS DB Instance connectivity error

I have created DB instance and gone through the config process of aws rds instance as per the documentation.
created an instance of db successfully.
also changed the connection params in database.yml accordingly.
I ran the following commands in ngnix server installed on ubuntu 16.04 at aws and also rebooted the db instance several times.
RAILS_ENV=production rake db:create
and db created successfully
and then ran
RAILS_ENV=production rake db:migrate
and database migrated successfully.
an then ran the command
sudo service nginx restart
after all of the process
when I am visiting my app its says
could not connect to server: Connection refused Is the server running on host "localhost" ( and accepting TCP/IP
connections on port 5432?
here is my database.yml
host: localhost
database: ups_developement_db
adapter: postgresql
encoding: unicode
port: 5432
username: postgres
password: postgres
database: ups_db
adapter: postgresql
encoding: unicode
port: 5432
username: dbmasteruser
password: dbmasterPassword
I am open to provide any kind of information required.
Thanks a lot for your time and solution in advance
It's almost certain that you have not configured the security groups on the RDS instance correctly.
Log into your EC2 and issue the command
telnet <rds hostname> 5432
You should see something like
Connected to
Escape character is '^]'.
which means that TCP can flow between your EC2 and your RDS.
If not, you need to permit your EC2 to connect to your RDS instance by adding the correct security group configuration.
You should create Environment variables in your AWS RDS instance you should use them in your rails app config file. Check here
database.yml should look like this
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>

setting ip address of database on different server

I deployed a sample Rails app to a remote server with postgres hosted on the same server. The database.yml file was like this.
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: localhost
username: mrmann
password: secret
Everything worked fine. I then went into the database.yml file and replaced host: localhost with the ip address of a postgres database on another server host: 178.XXX.XXX like this
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: 178.XXX.XXX.XXX #ip address of server with other postgres database
username: mrmann
password: secret
When I restarted postgres on the server with the Rails app, the sample application now gave me the 'something went wrong' page for Rails.
The username and the password for the username are the same on both dbs. Can you suggest what the problem might be? Thanks
These are the settings in pg_hba.conf on the server with the database that I want to connect to.
local all postgres peer
local all all peer
host all all md5
host all all ::1/128 md5
Fake Rails app IP: 192.241.XXX.X
Fake Database IP: 192.34.XX.XXX
Do you have access to the database logs? If your connection got as far as attempting to connect to the database, there should be some messages there. My WAG is that you need to add that IP to server's pg_hba.conf file and reload Postgres.
To be more clear, the IP to add to the server's pg_hba.conf is the IP that your application is connecting from, and you should also check the value of the database's listen_addresses setting. If the latter is not set to '*' or to the IP of your application, you'll need to change it in the postgresql.conf and restart the db cluster.
[edit, new information provided]
The line in your pg_hba.conf should be
host all all 192.241.XXX.X md5
Reload (not restart) postgres (eg. pg_ctl reload) and then try connecting again. If it fails, find the db log and see what it shows. If there is no message, then it might be a firewall problem. If the latter is suspected, look for any rules relating to port 5432 in the output of iptables -nvL

can't connect to rds at ec2 server, missing mysql socket

I have an ec2 server and rds server.
and a Ruby On Rails App
connecting to the rds with these settings worked for me in local ENV:
adapter: mysql2
encoding: utf8
reconnect: false
database: mainDb
pool: 20
username: root
password: xxxx
socket: /var/run/mysqld/mysqld.sock
port: 3306
but on my EC2 server I don't have that mysqld.sock file
so i get this error:
FATAL: failed to connect to MySQL (error=Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2))
what do i need to install in order to have the socket?
I removed the socket definition and the port.
I deploy using capistrano , now i ssh to my server and go to the "current" folder. there i try to run: rake ts:start
and i get the following:
rake aborted!
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
but i don't even have the socket definition in my database.yml file anymore
Remove the socket and port number from your database.yml file and then try, it will work.
You must specify the "host" of your RDS, and also configure the security groups in the RDS to allow your instance to connect to it.
I know this has been a while, just encountered this issue with my EC2 server connecting to RDS. Would like to share how I resolve this.
First removing socket and port from the database.yml help
#In your database.yml
#some configuration
#some configuration
adapter: mysql2
encoding: utf8
reconnect: false
pool: 20
database: your-database
username: your-username
password: your-password
To run this rake command on production, you need to specify your rails-env, e.g. if you are using production environment, it should be
bundle exec rake ts:start RAILS_ENV=production