Installing PostgreSQL on a mac (Lion)

Check to see if you mac already has an older version of PostgreSQL installed:

postgres –version

which postgres

If you see anything, that means you already have an older version of postgres installed.

Install PostgreSQL via Home Brew:

brew install postgresql

Somewhere in the output after PostgreSQL is installed you’ll see the version of your PostgreSQL you just installed.

Note the version of PostgreSQL you’ve installed. You’ll use it below.

Create Database Cluster

initdb /usr/local/var/postgres

Create LaunchAgents directory if it doesn’t exist already

mkdir -p ~/Library/LaunchAgents

Setup file that allows you to Stop/Start the Postgres Server

cp /usr/local/Cellar/postgresql/[your postgres version]/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/

Do the following if you already have a version of PostgreSQL installed so that instead the latest version of PostgreSQL will be used instead. (we will install the latest version below)

Edit your ~/.bash_login and add/modify so that /usr/local/bin/ loads first since that’s the location the new version of PostgreSQL will be copied to after you install it below.

PATH=/usr/local/bin:$PATH

open a new terminal or source your ~/.bash_login to pick up the new PATH.

Start up PostgreSQL server when your system boots

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Create Database User

createuser your_db_user_name

Shall the new role be a superuser? (y/n) n

Shall the new role be allowed to create databases? (y/n) n

Shall the new role be allowed to create more new roles? (y/n) n

Create Development and Test Databases

createdb -Oyour_db_user_name -Eutf8 your_project_development

createdb -Oyour_db_user_name -Eutf8 your_project_test

Set Postgres database passwords for Development Database

$ psql -U your_db_user_name your_project_development

psql (9.1.3)

Type “help” for help.

your_project_development=> ALTER USER your_db_user_name WITH PASSWORD ‘whateverpasswordyouwant’;

your_project_development=> \q

Set Postgres database passwords for Test Database

$ psql -U your_db_user_name your_project_test

psql (9.1.3)

Type “help” for help.

your_project_test=> ALTER USER your_db_user_name WITH PASSWORD ‘whateverpasswordyouwant’;

your_project_test=> \q

To Connect to Database (similar to mysql command)

psql -U your_db_user_name your_project_development

(to exit psql, type: \q)

Reinstall Gem pg so it can use the latest postgreSQL

gem uninstall pg

bundle or gem install pg

config/database.yml for Development database:

development:
 adapter: postgresql
 encoding: unicode
 database: your_project_development
 pool: 5
 username: your_db_user_name
 password: whateverpasswordyouwantfromabove

config/database.yml for Test database:

development:
 adapter: postgresql
 encoding: unicode
 database: your_project_test
 pool: 5
 username: your_db_user_name
 password: whateverpasswordyouwantfromabove
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)
Facebook Twitter Email

Leave a Reply