# Connection to postgresql failing

**URL:** <https://www.ruby-forum.com/t/connection-to-postgresql-failing/141488>\
**Category:** Ruby\
**Created:** [June 26, 2008, 10:56am UTC](https://www.ruby-forum.com/t/connection-to-postgresql-failing/141488 "2008-06-26T10:56:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 26, 2008, 10:56am UTC](https://www.ruby-forum.com/t/connection-to-postgresql-failing/141488/1 "2008-06-26T10:56:43Z")

</div>

I’ve been looking through all the posts relating to this and my syntax  
looks correct but my ruby file will not connect to my postgresql db.

I’m running this on an eeepc using PostgreSQL 7.4 with Ruby 1.9

Here is the code I’m using:  
require ‘postgres’  
conn = PGconn.connect(“localhost”, 5432, ‘’,’’, “winesite\_development”,  
“user”, nil)

Here is the error I’m getting:  
./convert.rb:12:in `connect’: could not connect to server: Connection  
refused (PGError)  
Is the server running on host “localhost” and accepting  
TCP/IP connections on port 5432?  
from ./convert.rb:12

I’ve checked that postgres is running on the machine and I can connect  
to it from the command line as user ‘user’ with no password.

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 26, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/connection-to-postgresql-failing/141488/2 "2008-06-26T18:58:38Z")

</div>

If the above fails, you can also try postgres-pr; it doesn’t require  
any compiled C, but should still work with your old pg. Keep in mind  
you’ll still need allowed access in your pg\_hba.conf.

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [June 26, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/connection-to-postgresql-failing/141488/3 "2008-06-26T16:10:39Z")

</div>

Michael Pope writes:

> I’ve been looking through all the posts relating to this and my  
> syntax  
> looks correct but my ruby file will not connect to my postgresql db.
> 
> I’m running this on an eeepc using PostgreSQL 7.4 with Ruby 1.9
> 
> Here is the code I’m using:  
> require ‘postgres’  
> conn = PGconn.connect(“localhost”, 5432, ‘’,‘’,  
> “winesite\_development”,  
> “user”, nil)
> 
> Here is the error I’m getting:  
> ./convert.rb:12:in `connect’: could not connect to server: Connection  
> refused (PGError)  
> Is the server running on host “localhost” and accepting  
> TCP/IP connections on port 5432?  
> from ./convert.rb:12
> 
> ## I’ve checked that postgres is running on the machine and I can connect to it from the command line as user ‘user’ with no password.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Just because PostgreSQL is running does not mean it will accept just  
any connection. PostgreSQL has access controls (network as well as  
user) which are configured in the ‘pg\_hba.conf’ file located in your  
PostgreSQL data directory. You can also check whether PostgreSQL is  
actually listening on port 5432 with netstat:

netstat -anf inet | grep 5432

(or for Linux: netstat -ant | grep 5432 )

If you don’t see an entry, or the listen posted is for a non-localhost  
address, that would be why you couldn’t connect. But in any case,  
change your code to this for now:

conn = PGconn.connect(nil, nil, ‘’,‘’, “winesite\_development”, “user”,  
nil)

This will force the postgres module to use Unix sockets to connect to  
PostgreSQL, which is most likely what ‘psql’ is using to connect to  
the database.

Of course, Unix sockets will not work from a remote system, so if you  
really need to use network sockets, you will need to coordinate the  
configuration of ‘pg\_hba.conf’ with your needs.

---

<div class="post-metadata">

**Author:** ![mariachi77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@mariachi77](https://www.ruby-forum.com/u/mariachi77)\
**Post date:** [July 8, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/connection-to-postgresql-failing/141488/4 "2008-07-08T16:33:34Z")

</div>

On Thu, 2008-06-26 at 17:54 +0900, Michael Pope wrote:

> I’ve checked that postgres is running on the machine and I can connect  
> to it from the command line as user ‘user’ with no password.

Connecting using:  
$ psql winesite\_development user  
is using a unix local domain socket, whereas:  
PGconn.connect(“localhost”, 5432, ‘’,’’,“winesite\_development”,“user”,  
nil)  
is using a socket connection to 127.0.0.1.

You can either configure PostgreSQL to accept connections to localhost,  
or you can connect using something like:  
PGconn.connect("/tmp", 5432, ‘’,’’,“winesite\_development”,“user”, nil)  
where “/tmp” is the same as the configuration parameter  
“unix\_socket\_directory” in postgresql.conf.

Or, if you’re using the newest “pg” driver (gem install pg), you can do:  
PGconn.connect(:dbname=\>‘winesite\_development’, :user=\>‘user’)

Regards,  
Jeff D.
