Anthony Myers
Anthony Myers

Reputation: 401

Cannot connect to Postgresql server, locally

I have a Rails app that has been using sqlite3 for the DB. Deployed to Heroku. Then find out that Heroku recommends switching to PostgreSQL. So now I'm trying to switch over without any luck. Was trying to use this Railscasts video for help.

I installed Homebrew. Installed PostgreSQL via Homebrew. After installation, there was no mention of creating a username or password for postgres.

I edited my Gemfile to

gem 'pg'

for both production and development and did bundle install.

I edited my database.yml file to this:

development:
  adapter: postgresql
  database: isement_dev
  encoding: unicode
  pool: 5
  timeout: 5000

test:
  adapter: postgresql
  database: isement_test
  encoding: unicode
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: isement_production
  encoding: unicode
  pool: 5
  timeout: 5000

Like Ryan says to do in the video, I try this command:

rake db:create:all

Only to get this error:

could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

I do some more searching, and see that some tutorials show username and password included in the database.yml file. I then find out how to setup a user for Postgresql

After entering in the command $ createuser joe, I was never given the options that the docs say you'll be asked. Such as "Shall the new role be a superuser? (y/n)" So really not sure if the user was created, but there wasn't any errors either.

So, I'm assuming, after creating the user "joe", I reedited my database.yml file to include the user field I just created:

 development:
   adapter: postgresql
   database: isement_dev
   encoding: unicode
   pool: 5
   timeout: 5000
   username: joe
   password:

 test:
   adapter: postgresql
   database: isement_test
   encoding: unicode
   pool: 5
   timeout: 5000

Only to still get the same error of not being able to connect.

I've ran the command

 pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

to make sure the server is running as well.

Just in case it's needed, when I run

which psql

I receive this:

 /usr/local/bin/psql

Is there something that I'm missing? The "database name" part of the database.yml file. This is supposed to be a database already created somewhere, or does this file create the database when I run the rake db:create:all command? I'm assuming the latter, so the name of the database doesn't matter?

Upvotes: 4

Views: 558

Answers (2)

Nikolai Manek
Nikolai Manek

Reputation: 1008

when you are installing Postgres via Homebrew it will add your username to postgres with an empty password.

Just open a command prompt and type

whoami

Then change your database.yml to your mac user name (whatever is returned from whoami) with empty password.

Maybe add host: localhost to you database.yml

Upvotes: 0

colinm
colinm

Reputation: 4288

Lazy option: Add host: localhost to your database.yml.

Only slightly less lazy option: Uninstall and reinstall the pg gem.

What's going on here? There are a number of ways for Postgres to already exist on your system, and the pg gem will use their pg_config output and build for their needs if you install the gem before installing your own copy of Postgres.

In your case, it was built for the version included with some releases of Mac OS X, which uses a socket file at /var/pgsql_socket.

Upvotes: 4

Related Questions