Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to connect to my Postgres instance on a local IP, which I can connect to with no problem using the psql -h localhost command. However, which trying to run via rails, I get this:

$ rails c production
/home/avishai/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could not connect to server: Connection refused (PG::Error)
    Is the server running on host "" and accepting
    TCP/IP connections on port 5432?

Here's my /etc/postgresql/9.1/main/pg_hba.conf:

# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
# 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               trust
host    all             all            trust
host    all             all            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

How do I make sure that Postgres can accept incoming connections from localhost as well as on select internal IPs?

share|improve this question
The "address" column is for the originating address, not the local IP. – John Dec 27 '12 at 19:40
Is postgresql actually running? – Michael Hampton Dec 27 '12 at 19:43
Yes, it definitely is. I can connect via psql – Avishai Dec 27 '12 at 19:46
Could it be a firewall thing? I am running iptables (it would appear) – Avishai Dec 27 '12 at 19:49
Yes, so what does your firewall look like? – Michael Hampton Dec 27 '12 at 20:18

2 Answers 2

up vote 4 down vote accepted

Please, try to check this two points:

1) In the file /etc/postgresql/9.1/main/postgresql.conf you have to have this line:

listen_addresses = '*'

instead of

listen_addresses = ''

You can check it via command netstat -tpln | grep 5432. You will see in the output somethink like:

tcp  0  0* LISTEN 1150/postgres

If the first IP address is, postgres is listening on the all interfaces, which is, what you want. If you will see in the first IP address field only, postgresql is listening only on the localhost interface.

2) Try to check your firewall rules via iptables -L -n. In the INPUT chain there must be rule accepting connection to port 5432 from IP adress of server, where are running rails. You can add this rule via command: iptables -I INPUT -p tcp -s --dport 5432 -j ACCEPT

But I think, that problem will be in the point 1).

share|improve this answer
Great call on listen_addresses = '*'! I opened it up and added the firewall rule and now things seem to be working quite nicely. – Avishai Dec 28 '12 at 1:58
listen_addresses = '' is the default setting in the Ubuntu server. It's more secure and if you would play only on the localhost, it is enough... I can advice you to remove from your pg_hba.conf file line: host all all trust. Line, which ended with md5 is sufficient. – Jan Marek Dec 28 '12 at 9:49

I had that same trouble.

In my case, it was using the IPV6 Localhost for some reason.

Try this in your pg_hba.conf:

host    all             all             ::1/128                 md5
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.