0

I have a Ruby on Rails application running on nginx server (application server is on Red Hat OS). The database is installed on another server (same OS: Red Hat). From console of the application server, I'm able to connect to the database using same db credentials as in application. But the ruby application is not able to make a connection. It is throwing error

The error log /var/log/nginx/error.log has below error message

stderr: 
PG::ConnectionBad (could not connect to server: Permission denied
      Is the server running on host "10.178.61.135" and accepting
      TCP/IP connections on port 5432?
):

Also, to mention I'm able to run the rake db:migrate successfully

The database.yml file looks like below:

production:
  adapter: postgresql
  encoding: unicode
  database: applicationdatabase
  host: 10.178.61.135
  pool: 30
  port: 5432
  timeout: 10000
  username: dbuser
  password: dbpassword

On Database server the connection config /var/lib/pgsql/11/data/pg_hba.conf:

# Allow replication connections from localhost, by a user with the replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
host       all          all             0.0.0.0/0               md5 

Database privilege

 List of databases
    Name             |    Owner    | Encoding |   Collate   |    Ctype    |      Access privileges
---------------------+-------------+----------+-------------+-------------+-----------------------------
 applicationdatabase | dbuser      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/dbuser            +
                     |             |          |             |             | dbuser=CTc/dbuser

Other configs and versions

# Web app Server
nginx/1.10.1
Phusion passenger 5.0.29
Ruby 2.1.9
Rails 4.1.5
pg 0.17.1
RHEL 7.7

# DB Server
PostgreSQL 11.3
RHEL 7.7
Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
  • Worth to mention, I read https://stackoverflow.com/questions/38466190/cant-connect-to-postgresql-on-port-5432 and the postgresql conf is as suggested in the post. – Swatantra Kumar Jan 13 '20 at 15:54
  • As suggested on https://stackoverflow.com/questions/19828385/pgconnectionbad-could-not-connect-to-server-connection-refused; I tried removing the PID file and rebooting the machines, but no luck – Swatantra Kumar Jan 13 '20 at 15:55
  • can try your pg_hba.conf `local all all trust` `host all all 127.0.0.1/32 md5` and `host all all ::1/128 md5` – Kamal Panhwar Jan 13 '20 at 17:00

1 Answers1

0

If 'psql' on the app server can connect but your application itself cannot, it sounds like something on the app server machine, like selinux or apparmor, which controls which users or apps can use the network.

jjanes
  • 37,812
  • 5
  • 27
  • 34