Alex Popov

My personal blog about my experience with Ruby, Rails and many other things

Install Postgresql 9.2.4 on OS X Mountain Lion

| Comments

A quick way to get Postgres to work with your existing Rails app

There are plenty of instructions for installing PSQL on OS X Mountain Lion such as this or this. The problem here is that as of Mountain Lion Postgres opens a socket in /var/pgsql_socket, which is different from previous versions. As a result, when you try to start Postgres with psql in the shell, you'll get an error message, similar to:

[PSQL starting error]
psql: 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"?

The links I mentioned above provide a possible solution to this problem. You could also try the varios suggestions in Stackoverflow. Alas, I had no luck with any of them, but this is what saved my day:

  1. Install the Postgress App:
  2. Amend your PATH by running this in the shell:

    export PATH="/Applications/$PATH"

  3. Start the from your Applications folder. You should be seeing a black elephant in the menu bar.

  4. Create a role with the same name as the one you have specified in your database.yml file with these commands

    • psql to startthe psql.
    • Create the role with CREATE ROLE <username> SUPERUSER LOGIN;.
    • \q to quit the psql shell.
  5. After that run these commands to recreate the development and environment databases of your app:

    • rake:db:create:all
    • rake:db:migrate
    • rake:db:migrate RAILS_ENV=test

You should be good to go.

P.S. Also check out this #342 Migrating to PostgreSQL Railscast for a full guide on migrating from SQLite to Postgres and this Stackoverflow question for similar instructions for Ubuntu.