Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql adapter (pg): could not connect to server

I get this error every this I run my Rails app (It cannot connect to my local Postgresql)

/Users/leonardo/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/ active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize':  could not connect to server: No such file or directory (PG::Error)    Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? 

I'm using Postgres.app that it's correctly running.

If I run

$ psql 

I can login properly to Postgresql console.

$ which psql  /Applications/Postgres.app/Contents/MacOS/bin/psql 

Gemfile

source 'https://rubygems.org' ruby "1.9.3"  gem 'rails', '3.2.11' gem "pg" 

database.yml

development:   adapter: postgresql   encoding: unicode   username: leonardo   password:    database: zapping   port: 5432   

Postgresql (Console)

$ psql leonardo=# \l 

enter image description here

like image 975
sparkle Avatar asked Jan 08 '13 22:01

sparkle


People also ask

Can't connect to Postgres server?

“Could not connect to server: Connection refused” To be sure that PostgreSQL is running, you can also restart it with systemctl restart postgresql. If this does not fix the problem, the most likely cause of this error is that PostgreSQL is not configured to allow TCP/IP connections.

Can't connect to Postgres server Windows?

First, double check that the Postgres process is running where you expect it to be. If you are trying to connect to a Postgres instance on the same host as your terminal, you can run lsof -p :5432 which will show which, if any, processes are listening on that port. The postgres process should be connected there.

Can't connect to server No such file or directory Postgres?

When connecting to Postgres you might see this error: psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket <some socket> . This happens most often when Postgres' server daemon process is not running.


2 Answers

Try adding host: localhost to your database.yml. (Based on: https://stackoverflow.com/a/10793186/919641)

like image 128
pjam Avatar answered Sep 27 '22 18:09

pjam


Your Pg gem was compiled against the PostgreSQL libpq pre-installed in Mac OS X and you're using the psql that you installed in a newer version, or vice versa.

This can be worked around by specifying a TCP/IP connection, by adding localhost to database.yml, but it's better to compile the Pg gem against the libpq for the server you're actually running. To do that, you should be able to set the PATH environment variable to the folder with the correct pg_config in it before compiling. In your case that'll be somewhere within Postgres.app.

like image 25
Craig Ringer Avatar answered Sep 27 '22 18:09

Craig Ringer