Connect AWS Red Shift with Rails Project - ruby-on-rails

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 "...us-west-2.redshift.amazonaws.com" (IP) and accepting TCP/IP connections on port 5439?"
Database configuration:
development:
adapter: redshift
host: '....us-west-2.redshift.amazonaws.com'
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
Destination 127.0.0.1

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

Related

Cannot connect docker container on ECS Fargate to RDS db instance

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
production:
<<: *default
host: <%= ENV['DATABASE_URL'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
database: my_project_db
where
DATABASE_URL=myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com
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: myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com. Which can be found in the database configuration file located at config/database.yml
PG::ConnectionBad: could not translate host name "myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com" 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?
UPDATE
My VPC has DNS resolution enabled
I span up an EC2 instance and did nslookup and here's the output
nslookup myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com
Server: 10.0.0.2
Address: 10.0.0.2#53
Non-authoritative answer:
myprojectdbinstance.c42n4hggvli1.eu-central-1.rds.amazonaws.com canonical name = ec2-18-193-32-4.eu-central-1.compute.amazonaws.com.
Name: ec2-18-193-32-4.eu-central-1.compute.amazonaws.com
Address: 10.0.1.233
DB security group (it allows access from sg-047f1acd163d6f380)
sg-047f1acd163d6f380 security group that I set to my ECS task

How to connect postgresql database on remote server rails

I'm trying to connect to a remote server from my local machine. Here's how my database.yml file looks like:
development:
adapter: postgresql
encoding: unicode
database: db_name
username: mac
password: mac
host: 178.XXX.XXX.XXX
port: 5432
pool: 10
timeout: 5000
I edited pg_hba.conf to take my ip address:
local all all trust
# IPv4 local connections:
host all all 35.XXX.XX.XX/32 trust
# IPv6 local connections:
host all all ::1/128 trust
Remote server is hosted on AWS with centos 6 AMI. Below inbound rules are added in it's security group.
But I keep getting below error:
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "<HOST NAME>" (178.XXX.XXX.XXX) and accepting
TCP/IP connections on port 5432?
For logs checked /var/lib/pgsql/9.6/data/pg_log, however no logs are being created.
Running netstat -ntlp tells that server is listening on 5432 port
Am I missing something or doing something wrong here?
please help
Check my answer from yesterday. There is a procedure how find a problem with connecting to the remote PostgreSQL database:
Remote PostgreSQL connection
The error you have posted is not related to Ruby on rails. It is standard problem with wrong PostgreSQL settings. First try psql which should show the same error as you posted
Write a comment if you will have a problem. I bet that the problem is because you forgot set listen_address"'*' in postgresql.conf because you don't mention this in the answer.

PostgreSQL: could not connect to server: Connection timed out

I've been trying to make the switch to PostgreSQL (from SQLite). I'm developing in the environment of Cloud9. However, on db migration I get the error message:
PG::ConnectionBad: could not connect to server: Connection timed out.
Is the server running on host "mydomain.c9.io" (IP address) and
accepting TCP/IP connections on port 5432?
I've been reading all sorts of previously asked questions on this topic but without success. Does anyone see what might be going wrong?
Do I perhaps need to makes change to the pg_hba.conf file? The current version of the file:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all xxx.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres xxx.0.0.1/32 md5
#host replication postgres ::1/128 md5
database.yml:
default: &default
adapter: postgresql
encoding: unicode
host: 'mydomain.c9.io'
pool: 5
username: my_username
password: my_password
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
I have gem 'pg' in the Gemfile and have run bundle install. Also, I have created the database app_development (used sudo service postgresql start, sudo sudo -u postgres psql and then create database "app_development";) as well as the database app_test. And lastly, I created a new user in psql with CREATE USER my_username SUPERUSER PASSWORD 'my_password';.
Using sudo nano /etc/postgresql/9.3/main/postgresql.conf I also set listen_addresses = '*' but that made no difference.
What might be causing the error message?
Are you sure that your cloud 9's has postgres port(5432) open for access? I assume the cloud 9 host is not listening on port 5432. You can check out this link for detais
http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html

Postgres database has no password - can't get into pgadmin

I have a rails app using postgres. When I made the app, I never created a password. In my database.yml file, no password is listed. My application works, but I am trying to connect to the db using pg_admin so I can see the tables. When I try to connect, I am given the following:
Server doesn't listen
The server doesn't accept connections: the connection library reports
could not connect to server: Connection refused Is the server running on host
"localhost" (::1) and accepting TCP/IP connections on port 3000?
How can i fix this? If it helps, here is the code from my database.yml file for development:
development:
adapter: postgresql
encoding: unicode
database: supersmart_dev
pool: 5
username: postgres
host: localhost

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.
production:
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
production:
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
Update
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 127.0.0.1/32 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

Resources