Sunday, July 7, 2013

Installing Postgres on Mac OS

My notes on installing Postgres on my MacBook Pro (Snow Leopard)


I use MacPort to install Postgres. See  http://www.macports.org/ to get the installer.
To install Postgres using Port:

@mbp /opt/local$ sudo port install postgresql92-server
@mbp /opt/local$ sudo port install postgresql92

Check that they're installed:
@mbp ~/dev/play$ port installed | grep postgres
  postgresql92 @9.2.4_0 (active)
  postgresql92-server @9.2.4_0 (active)
  postgresql_select @0.1_0 (active)

The installer creates "postgres" users by default.

Create a postgres database
@mbp /opt/local$  sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/db/postgresql92/defaultdb' 

postgres$ ls -Fal /opt/local/var/db/postgresql92/defaultdb/
total 80
drwx------  19 postgres  postgres    646 Jul  4 15:49 ./
drwxr-xr-x   3 root      admin       102 Jul  4 15:41 ../
-rw-------   1 postgres  postgres      4 Jul  4 15:41 PG_VERSION
drwx------   5 postgres  postgres    170 Jul  4 15:41 base/
drwx------  42 postgres  postgres   1428 Jul  4 15:49 global/
drwx------   3 postgres  postgres    102 Jul  4 15:41 pg_clog/
-rw-------   1 postgres  postgres   4476 Jul  4 15:41 pg_hba.conf
-rw-------   1 postgres  postgres   1636 Jul  4 15:41 pg_ident.conf
drwx------   4 postgres  postgres    136 Jul  4 15:41 pg_multixact/
drwx------   3 postgres  postgres    102 Jul  4 15:44 pg_notify/
drwx------   2 postgres  postgres     68 Jul  4 15:41 pg_serial/
drwx------   2 postgres  postgres     68 Jul  4 15:41 pg_snapshots/
drwx------   2 postgres  postgres     68 Jul  4 15:49 pg_stat_tmp/
drwx------   3 postgres  postgres    102 Jul  4 15:41 pg_subtrans/
drwx------   2 postgres  postgres     68 Jul  4 15:41 pg_tblspc/
drwx------   2 postgres  postgres     68 Jul  4 15:41 pg_twophase/
drwx------   4 postgres  postgres    136 Jul  4 15:41 pg_xlog/
-rw-------   1 postgres  postgres  19625 Jul  4 15:41 postgresql.conf
-rw-------   1 postgres  postgres     89 Jul  4 15:44 postmaster.opts

To Start:
postgres$ /opt/local/lib/postgresql92/bin/pg_ctl start -D /opt/local/var/db/postgresql92/defaultdb -l /opt/local/var/log/postgresql92/postgres.log 

To Stop:
postgres$ /opt/local/lib/postgresql92/bin/pg_ctl stop -D  /opt/local/var/db/postgresql92/defaultdb

Create a new db:
postgres$ /opt/local/lib/postgresql92/bin/createdb mydb

Create a new role/user called "elousf":
postgres$ /opt/local/lib/postgresql92/bin/psql mydb

mydb=# create role elousf login;
CREATE ROLE


Then as elousf, connect, create a table, and test a transaction:

@mbp ~$ psql mydb -U elousf
psql (9.2.4)
Type "help" for help.

mydb=> create table test_table (id integer, name varchar(64));
CREATE TABLE
mydb=> begin transaction;
BEGIN
mydb=> insert into test_table values (1, 'hello');
INSERT 0 1
mydb=> insert into test_table values (2, 'world');
INSERT 0 1
mydb=> select * from test_table;
 id | name  
----+-------
  1 | hello
  2 | world
(2 rows)

mydb=> rollback;
ROLLBACK
mydb=> select * from test_table;
 id | name 
----+------
(0 rows)

I got myself a Postgres database to play with!

No comments: