How to connect postgresql database on remote server rails - ruby-on-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.

Related

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 "...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

bundle exec rake db:create error

Keep getting this when I run bundle exec rake db:create:
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Any ideas how to fix this?
Thats probably because of a permission issue. Navigate to /etc/postgresql/<your version of pg>/main/ and open up pg_hba.conf.
There search for a line that'd say
local all postgres peer and replace it to local all all trust.
Then run sudo service postgresql restart in your terminal.
Try this :
It will work
your database.yml should look like this :
development:
adapter: postgresql
encoding: unicode
database: <DATABASE_NAME>
pool: 5
username: <username>
password: <password>
host: localhost
port: 5432
And it will be same for another environments

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