Skip to content

Instantly share code, notes, and snippets.

@mdominiak
Created September 16, 2011 20:36
Show Gist options
  • Save mdominiak/1223088 to your computer and use it in GitHub Desktop.
Save mdominiak/1223088 to your computer and use it in GitHub Desktop.
Setting up and Using PostgreSQL for Ruby on Rails development on OS X

Installation

brew update
brew install postgresql

Create a database:

initdb /usr/local/var/postgres

If this is your first install, automatically load on login with:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist

If this is an upgrade and you already have the org.postgresql.postgres.plist loaded:

launchctl unload -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist

Verify if it is running:

ps auxwww | grep postgres

Install pg gem:

env ARCHFLAGS="-arch x86_64" gem install --no-ri --no-rdoc pg

Create a user

Create a user that will let use rake db:create with default config/database.yml:

createuser psql
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n

Other Tasks

Create a new Rails project using PostgreSQL

rails new vault106 -d postgresql
bundle exec rake db:create

Run database console

psql -U psql vault106

List all tables:

db> \dt

List table details:

db> \d posts

Hit CTRL+D to exit console, or:

db> \q

Create a database

createdb -Ovault106 -Eutf8 vault106_production

Create database dump/restore database from dump

TBA

Manually start/stop

Start manually with:

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

And stop with:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment