[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Connection to postgresql failing

Michael Pope

6/26/2008 8:55:00 AM

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

3 Answers

Coey Minear

6/26/2008 2:06:00 PM

0

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

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.


--
Coey Minear

alandacosta@gmail.com

6/26/2008 4:54:00 PM

0

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.

Jeff Davis

7/8/2008 2:29:00 PM

0

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 Davis