Chris's Blog

Keep Walking......

PostgreSQL Installation on MAC

  1. Use MacPorts install PostgreSQL server

     sudo port install postgresql93-server
    
  2. 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  
    
  3. 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'
    
  4. 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.

  5. Create User

     createuser --superuser Chris -U postgres
    
  6. 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"?
    
  7. Accessing Database

     psql mydb
    

    mydb=# means you are a database superuser, otherwise display mydb=>.

  8. Exit psql

     mydb=> \q
    
  9. 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'
    

Comments