Home > lion, mac osx, postgresql > Install Postgres on Mac OS X Lion with homebrew howto

Install Postgres on Mac OS X Lion with homebrew howto

1) This is the easiest part, simply

$ brew install postgres

2) this step is not obvious but you can’t move on without it (would be nice if brew can automate this step too :D)

$ initdb /usr/local/var/postgres

3) add /usr/local/bin to $PATH if it’s not already there, one way to do that is

$ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile && . ~/.bash_profile

without this step you will end up using /usr/bin/psql, which is installed by default in Lion, while trying to access db, which would give you the following 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”?

[added 7/26/2012]
3.5) start postgres server

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

4) access postgres db

psql postgres your_username

or

psql postgres `whoami`

assuming your_username is the one who runs step 2

5) if everything’s ok you should be able to see

psql (9.1.3)
Type “help” for help.

postgres=#

type \q to exit

6) if you want to create your first db, do (under system shell, not postgres) a

createdb mydb

and access it through

psql mydb your_username

Note: your_username in command psql is optional if the shell user is the creator of the db to be accessed, that is, you can simply type psql postgres or psql mydb to access the dbs.

Enjoy.

References:
http://archives.postgresql.org/pgsql-general/2009-09/msg00990.php
http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/ (didn’t use the method describe in this post but use the PATH method instead)
http://www.postgresql.org/docs/9.0/static/tutorial-createdb.html

Advertisements
Categories: lion, mac osx, postgresql
  1. November 24, 2012 at 12:30 pm

    This worked for me (after removing mac ports, which brew complained about), but then I found I had no expected default ‘postgres’ user; the only (super)user was my own user account. After I understood that and got to a psql prompt, to add the postgres user:
    create role postgres with password ‘postgres’ superuser login;

  2. oldfartdeveloper
    July 16, 2013 at 5:25 pm

    Very helpful, thanks.

  3. herrings
    September 16, 2016 at 1:49 pm

    Works like a charm! Thank man…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: