From: Coey Minear Date: 2008-06-26T23:06:06+09:00 Subject: Re: Connection to postgresql failing 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/. > 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