Take the 2-minute tour ×
Server Fault is a question and answer site for professional 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 "10.61.99.194" and accepting
    TCP/IP connections on port 5432?

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

# DO NOT DISABLE!
# 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             127.0.0.1/32            trust
host    all             all             10.80.85.130/32         trust
host    all             all             10.80.85.130/32         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
1  
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 = '127.0.0.1'

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

tcp  0  0  0.0.0.0:5432  0.0.0.0:* LISTEN 1150/postgres

If the first IP address is 0.0.0.0, postgres is listening on the all interfaces, which is, what you want. If you will see in the first IP address field only 127.0.0.1, 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 10.80.85.130 --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
1  
listen_addresses = '127.0.0.1' 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 10.80.85.130/32 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

 
discard

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.