Connection to postgresql failing

M

Michael Pope

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.
 
C

Coey Minear

Michael said:
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.

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.
 
A

alandacosta

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.
 
J

Jeff Davis

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 Davis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top