I installed postgresql on Mac using Homebrew, so I’ll introduce the steps.
Installing postgresql
First, install postgresql with the brew command.
For those who haven’t installed Homebrew, please refer to the following:
brew install postgresql
PostgreSQL Database Initialization
initdb /usr/local/var/postgres -E utf8 --no-locale
Auto-start Configuration
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Setting PGDATA Environment Variable
Setting the PGDATA environment variable eliminates the need to pass the -D argument when starting/stopping with pg_ctl.
export PGDATA=/usr/local/var/postgres
Start Command
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
# If you've set the PGDATA environment variable, you can abbreviate it as follows
pg_ctl -l /usr/local/var/postgres/server.log start
Stop Command
pg_ctl stop -s -m fast
# If you've set the PGDATA environment variable, you can abbreviate it as follows
pg_ctl -D /usr/local/var/postgres stop -s -m fast
Creating postgres User
When installed with brew, the postgres user is not created.
$USER is created as a superuser. Since it’s not good to use the superuser as-is, we’ll create a new user.
$ createuser -P username
# Replace username with the name of the new user to create
# Enter password and whether to make it a superuser
You can check if the user was created properly with the following command:
psql -q -c'select * from pg_user' postgres
Install pg with gem to use postgresql in Ruby.
env ARCHFLAGS="-arch x86_64" gem install pg
By the way, running the brew info postgresql command shows the following details:
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/mxcl/homebrew/issues/issue/2510
To build plpython against a specific Python, set PYTHON prior to brewing:
PYTHON=/usr/local/bin/python brew install postgresql
See:
http://www.postgresql.org/docs/9.0/static/install-procedure.html
If this is your first install, create a database with:
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
Or 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
Some machines may require provisioning of shared memory:
http://www.postgresql.org/docs/current/static/kernel-resources.html#SYSVIPC
If you want to install the postgres gem, including ARCHFLAGS is recommended:
env ARCHFLAGS="-arch x86_64" gem install pg
To install gems without sudo, see the Homebrew wiki.
Reference Information
- Installing postgresql on Mac with Homebrew - Hello, world! - s21g
- Installing pg with gem - Daily Laughter
- Mac+HomeBrew+Postgresql+pg - NowTom's Diary
- Installing Postgresql from Homebrew - Fish Chicken Bread.
- PostgreSQL Setup - Qiita
That’s all from the Gemba.