This is based off of the following sites:
- http://devcenter.heroku.com/articles/local-postgresql
- http://www.postgresql.org/docs/8.1/static/tutorial-start.html
- http://stackoverflow.com/questions/6770649/repairing-postgresql-after-upgrading-to-osx-10-7-lion
NOTE: If you have OSX Lion, you probably already have postgres; you may need to edit your $PATH if which psql
returns /usr/bin/psql
. Otherwise, you have to add -h localhost
to some commands to get them to work.
-
Install homebrew (popular package manager): http://mxcl.github.com/homebrew/
-
Install postgres (may take a while):
$ brew install postgresql
-
After the installation completes, there will be instructions on how to create a database, start the server, start the server at login, and install the pg gem. Do all of this (copy and paste each command)
-
Start the PostgreSQL interactive terminal:
$ psql postgres -h localhost
NOTE: since I'm on lion, I had to add the-h localhost
argument to all psql commands. -
Create a user and a database:
CREATE USER <username> SUPERUSER; CREATE DATABASE <database_name> OWNER <username>;
-
In your rails app, modify the database.yml as shown (replacing the db name and username), run dm migrations (if needed), and restart your server.
How to list all postgres databases:
$ psql postgres -h localhost -l
How to drop a database:
$ dropdb <mydb> -h localhost
Start postgres:
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Stop postgres:
$ pg_ctl -D /usr/local/var/postgres stop -s -m fast
Postres admin (GUI admin for postgres):