Instantly share code, notes, and snippets.

Embed
What would you like to do?
install PostgreSQL 9 in Mac OSX via Homebrew
install PostgreSQL 9 in Mac OSX via Homebrew
Mac OS X Snow Leopard
System Version: Mac OS X 10.6.5
Kernel Version: Darwin 10.5.0
Install notes for PostgreSQL 9.0.1 install using Homebrew:
sh-3.2# brew install postgresql
sh-3.2# brew install postgresql
Warning: It appears you have MacPorts or Fink installed.
Software installed with MacPorts and Fink are known to cause problems.
If you experience issues try uninstalling these tools.
==> Downloading ftp://ftp.ossp.org/pkg/lib/uuid/uuid-1.6.2.tar.gz
######################################################################## 100.0%
==> ./configure --disable-debug --without-perl --without-php --without-pgsql --p
==> make
==> make install
/usr/local/Cellar/ossp-uuid/1.6.2: 11 files, 364K, built in 17 seconds
==> Downloading http://ftp9.us.postgresql.org/pub/mirrors/postgresql/source/v9.0
######################################################################## 100.0%
==> ./configure --disable-debug --prefix=/usr/local/Cellar/postgresql/9.0.1 --enable-thread
==> make install
==> cd contrib/adminpack; make install
==> cd contrib/dblink; make install
==> cd contrib/fuzzystrmatch; make install
==> cd contrib/lo; make install
==> cd contrib/uuid-ossp; make install
==> cd contrib/pg_buffercache; make install
==> cd contrib/pg_trgm; make install
==> cd contrib/pgcrypto; make install
==> cd contrib/tsearch2; make install
==> cd contrib/vacuumlo; make install
==> cd contrib/xml2; make install
==> cd contrib/intarray; make install
==> cd contrib/pg_upgrade; make install
==> cd contrib/pg_upgrade_support; make install
==> cd contrib/hstore; make install
==> Caveats
If builds of Postgresl 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:
cp /usr/local/Cellar/postgresql/9.0.1/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.1/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
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.
==> Summary
/usr/local/Cellar/postgresql/9.0.1: 1219 files, 20M, built in 3.4 minutes
sh-3.2# exit
Init Database:
Erics-MacBook:~ eric$ initdb /usr/local/var/postgres
The files belonging to this database system will be owned by user "eric".
This user must also own the server process.
The database cluster will be initialized with locale en_US.UTF-8.
The default database encoding has accordingly been set to UTF8.
The default text search configuration will be set to "english".
creating directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers ... 2400kB
creating configuration files ... ok
creating template1 database in /usr/local/var/postgres/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.
Success. You can now start the database server using:
postgres -D /usr/local/var/postgres
or
pg_ctl -D /usr/local/var/postgres -l logfile start
Run Database:
Erics-MacBook:~ eric$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start &
Create a Database:
Erics-MacBook:~ eric$ createdb mydb
Open the mydb database and do some test:
Erics-MacBook:~ eric$ psql mydb
psql (9.0.1)
Type "help" for help.
mydb=# create table users(id serial, name varchar(25));
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
CREATE TABLE
mydb=# insert into users(name) values('eric');
INSERT 0 1
mydb=# insert into users(name) values('lxneng');
INSERT 0 1
mydb=# select * from users;
id | name
----+-------------
1 | eric
2 | lxneng
mydb=# \q
@abruzzi

This comment has been minimized.

abruzzi commented Apr 8, 2014

To summarize :

# install the binary
$ brew install postgresql

# init it
$ initdb /usr/local/var/postgres

# start the postgres server
$ postgres -D /usr/local/var/postgres

# create your database
$ createdb mydb

Then play around with it:

Erics-MacBook:~ eric$ psql mydb
psql (9.0.1)
Type "help" for help.

mydb=# create table users(id serial, name varchar(25));
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
CREATE TABLE
mydb=# insert into users(name) values('eric');
INSERT 0 1
mydb=# insert into users(name) values('lxneng');
INSERT 0 1
mydb=# select * from users;
 id |    name     
----+-------------
  1 | eric
  2 | lxneng
mydb=# \q
@jonmorehouse

This comment has been minimized.

jonmorehouse commented Apr 30, 2014

Also worth noting. Depending upon which versions of Postgresql you've had on your machine, sometimes you may need to clear out the old /usr/local/var/postgres folder. Running postgresql via launchtcl can sometimes fail silently if there is an old db folder from a different version.

# remove old database files (this is dangerous)
$rm -rf /usr/local/var/postgres

# install the binary
$ brew install postgresql

# init it
$ initdb /usr/local/var/postgres

# start the postgres server
$ postgres -D /usr/local/var/postgres

# create your database
$ createdb mydb

@bansalgagan

This comment has been minimized.

bansalgagan commented Jun 16, 2014

How to stop the server?

@whyvez

This comment has been minimized.

whyvez commented Jun 19, 2014

@bansalgagan pg_ctl stop

can pass mode as -m i[mmediate] -f[ast]

pg_ctl expects PGDATA env var to be set or you can pass the data directory via the -D param. You can find by your data dir by running SHOW data_directory; against the db via psql or your ui of choice.

for more info pg_ctl --help

@achikin

This comment has been minimized.

achikin commented Dec 18, 2014

Very useful guide, thank you!
How do I setup a user and password?

@kiote

This comment has been minimized.

kiote commented May 15, 2016

# Create user with password
createuser -P my_user_name

# Grant rights for just created user to database
psql my_db_name

# after connecting
GRANT ALL PRIVILEGES ON database my_db_name TO my_user_name;
@kodero

This comment has been minimized.

kodero commented Nov 2, 2017

Very helpful. Thank you!

@huoxy

This comment has been minimized.

huoxy commented Jan 10, 2018

Thanks a lot!

@tikdove

This comment has been minimized.

tikdove commented Jun 2, 2018

Great, it's very useful.

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