Sunday, 15 April 2012

PostgreSQL psql: could not connect to server: Connection refused



I have faced issue with my rails application whcih uses postgresql as the database. Here is the error I have received when I start my server.


/usr/local/rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:1161:in `initialize': could not connect to server: Connection timed out (PG::Error)
Is the server running on host "168.162.1.34" and accepting
TCP/IP connections on port 5432?


I came to see this is due to postgresql configuration issue. Recently my Ip has been changed to new one and need to modify the IP address with the new one in the postgresql configuration file.

postgresql configuration file exists at '/etc/postgresql/9.1/main/postgresql.conf'

You need to have root access to modify this file. Login as root user through terminal and go to the above mentioned path.

> gedit postgresql.conf

which will open the configuration file in edit mode in the gedit editor.

> listen_addresses = '168.162.5.34'

Modify the above line with your desired IP address.

save the file abd exit from the terminal from edit mode.

restart the server by below command

> sudo /etc/init.d/postgresql restart

By this way you can change the configuration of postgresql.

Thank You,
Uma Mahesh Varma.

1 comment:

  1. You need to modify the pg_hba.conf in the same path '/etc/postgresql/9.1/main/pg_hba.conf'

    Modify the below line to match with you ip

    host all all 192.168.2.143/24 md5


    restart the server to make you changes apply.

    Thank You,
    Uma Mahesh

    ReplyDelete