Tuesday, August 11, 2015

Set up PostgreSQL on OSX

Install

brew install postgresql
==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.4.4.yosemite.b
########################################### 100.0%
==> Pouring postgresql-9.4.4.yosemite.bottle.tar.gz
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
  https://github.com/Homebrew/homebrew/issues/2510

To migrate existing data from a previous major version (pre-9.4) of PostgreSQL, see:
  https://www.postgresql.org/docs/9.4/static/upgrading.html

To have launchd start postgresql at login:
  ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
  postgres -D /usr/local/var/postgres
==> Summary
/usr/local/Cellar/postgresql/9.4.4: 3014 files, 40M

Initialize

This will create a user (same name as the OS user) and initialize the data directory:
initdb ~/postgres

Create user (optional)

initdb has created a user for you, but if you want to create additional users (for example, Wordpress user), you can do the following:
# --pwprompt Password prompt
createuser --pwprompt newUser

Create a database

-O means owner.
createdb -Ouser testdb
Your login account (the default) will be used if no owner is specified.
createdb -Ouser testdb

Start/stop the SQL server

# use `stop` instead when you want to stop
pg_ctl -D ~/postgres/ start
# specify a logging file
pg_ctl -D ~/postgres/ -l ~/.postgre.log start

Access database

User login, with the testdb database we have created in the last step.
# -U: User
# -W: passWord
psql -U user -W testdb

0 comments: