Use MacPorts install PostgreSQL server
sudo port install postgresql93-server
Create Database Instance
sudo mkdir -p /opt/local/var/db/postgresql93/defaultdb sudo chown postgres:postgres /opt/local/var/db/postgresql93/defaultdb sudo su postgres -c '/opt/local/lib/postgresql93/bin/initdb -D /opt/local/var/db/postgresql93/defaultdb' sudo mkdir -p /opt/local/var/log/postgresql sudo chown postgres:postgres /opt/local/var/log/postgresql
Start Database Server
sudo su postgres -c '/opt/local/lib/postgresql93/bin/postgres -D /opt/local/var/db/postgresql93/defaultdb'
or
sudo su postgres -c '/opt/local/lib/postgresql93/bin/pg_ctl -D /opt/local/var/db/postgresql93/defaultdb -l /opt/local/var/log/postgresql/postgres.log start'
Set PATH environment variable
export PATH=/opt/local/lib/postgresql93/bin:$PATH
Make sure you set the PostgreSQL path before /usr/bin to ensure that you are using the latest versions and not the default Apple provided, otherwise will encounter error when execute operation.
Create User
createuser --superuser Chris -U postgres
Create Database
createdb mydb
If below error encountered, that caused by you use old version PostgreSQL which provide by Apple, change PATH environment variable to solve.
createdb: could not connect to database postgres: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Accessing Database
psql mydb
mydb=# means you are a database superuser, otherwise display mydb=>.
Exit psql
mydb=> \q
Shutdown Database Server
sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql93/defaultdb -l /opt/local/var/log/postgresql/postgres.log stop'