4

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Rather than host: localhost, you should IMO really recommend host: /tmp so it still uses UNIX sockets. (I'm quite annoyed that the Homebrew people didn't write their PostgreSQL package to also use /var/pgsq_socket to save users all this confusion - though of course Apple's the one who put the darn thing on the default PATH in the first place).
Thank you @colinm for taking the time! So I've removed the pg gem, did bundle install, added it back in, did bundle install again. Started up the server. Still getting the error, but it's slightly different now: isement_test already exists and then it says the same error from before. Is your answer either or? Because I have added in the host: localhost as well. Yeah, good call on installing the gem before installing the new version. That's exactly what I did.
Thank you @CraigRinger . I tried your answer and it's telling me this:This task only modifies local databases. isement_test is on a remote host. Which really confused me! How would my test database be on a remote host?!
0

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

2 Comments

Hmmm, ok. So changed my username in the database.yml file to my mac username. Resaved, quit server, restarted server. Tried "rake db:create:all" again, and still getting the same error. Could the database name be the issue?
Have you started the local Postgresql database/ I use "pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start". Pierre

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.