setting ip address of database on different server - ruby-on-rails

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

Related

Logs say "fe_sendauth: no password supplied" but rails console working without problems

I'm trying to deploy rails app with capistrano, nginx and puma. When I visit the app I get errors like this in my production.log:
[11c972c9-c8fb-404f-93c3-9fc614ad815b] ActiveRecord::ConnectionNotEstablished (connection to server at "localhost" (127.0.0.1), port 5432 failed: fe_sendauth: no password supplied
):
When I log to this machine and run bin/rails console it works without any problems, I can create records, query records. Connection to database works without problems.
RAILS_ENV is set to production in /etc/environment
Any idea what could be the problem?
database.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
production:
<<: *default
database: app_production
username: app
pg_hba.conf
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 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
The solution for me was to delete PostgreSQL completely using,
sudo apt-get --purge remove postgresql\*
Click yes on that pop-up, re-install PostgreSQL using,
sudo apt update
then,
sudo apt install postgresql postgresql-contrib
then,
sudo service postgresql start
Finally, you'll also need to create a database user so that you are able to connect to the database from Rails. First, check what your operating system username is:
whoami
If your username is "stack", for example, you'd need to create a Postgres user with that same name. To do so, run this command to open the Postgres CLI:
sudo -u postgres -i
From the Postgres CLI, run this command (replacing "stack" with your username):
createuser -sr stack
Then enter control + d or type logout to exit.

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

I can access postgres database on Rails even with a wrong password

I'm able to connect to my local Postgres database, even with a wrong password. Here is how I created a role and a database (I'm on Mac OSX with Postgres 9.3):
psql postgres
CREATE ROLE role_name WITH ENCRYPTED PASSWORD 'role_password' NOSUPERUSER NOCREATEROLE NOCREATEDB LOGIN;
CREATE DATABASE db_name OWNER role_name;
\q
So, when I configure my database.yml file on Rails, using the following:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: db_name
username: role_name
password: this_is_a_wrong_password_ha_ha
and starting the server (rails s), everything works fine: I can create tables with migrations and create/read/update/delete data in it.
I can even do that without giving a role and/or a password.
How can I block access to a database if the role name and role password are not correct? Where I'm wrong? Thanks
Seems like this is default settings for your /usr/local/var/postgres/pg_hba.conf. It is allow access to DB for any local connections:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust

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

Resources