Install postgresql 9.1 on Ubuntu 12.04


Another install guide - maybe not super friendly, but it worked.


1) Install

$ sudo apt-get update
$ sudo apt-get install postgresql-9.1

Verify it works
$ sudo invoke-rc.d postgresql status

2) Set password

Now you need to set a password for the postgres database role:
sudo -u postgres psql postgres
\password postgres

3) Install the contrib (contains ssl, text stuff and required stuff used by pgadmin)
Read more?: http://packages.debian.org/wheezy/postgresql-contrib-9.1

$ sudo apt-get install postgresql postgresql-contrib
$ sudo -u postgres psql

postgres=# CREATE EXTENSION adminpack;
CREATE EXTENSION


Verify that it was installed:
select * from pg_extension;


3) Optionally restart database server?

sudo /etc/init.d/postgresql restart

4) Create databases

Now create a database
postgres=# CREATE DATABASE mytestdb;
postgres=# \c mytestdb

mytestdb=# CREATE TABLE monkeytest (abe int);

Display the tables
mytestdb=# \d

Insert a row
mytestdb=# INSERT INTO monkeytest (abe) VALUES (77);

Show the rows:
mytestdb=# SELECT * FROM monkeytest;

For help use \h


The passwords would be from a local users (ident authentication mode), but we need to change this, as it's not practical.
$ sudo vim /etc/postgresql/9.1/main/pg_hba.conf

CHANGE THIS:
local   all             all                                     peer
INTO THIS:
local   all             all                                     md5

TO GET STARTED QUICKLY JUST CHANGE TO (only development). It will match all ip's on the host

host   all             all              0.0.0.0/0                md5


In production it could be:
host all all 10.10.29.0/24 trust


=== OPTIONALLY START ===

You also need to set the IP it will listen on like:
sudo vim /etc/postgresql/9.1/main/postgresql.conf 
listen_addresses = '*'

Or for production you would use something like:
listen_addresses='202.54.1.2 202.54.1.3'

(Optionally) Read more about authentication: http://www.postgresql.org/docs/9.1/static/auth-methods.html
=== OPTIONALLY END ===

sudo /etc/init.d/postgresql restart

5) Create user and test permissions

For using this database, create a new user:
$ sudo -u postgres createuser -P alison
(Optional: To drop again later:)
$ sudo -u postgres dropuser alison

$ sudo -u postgres psql
\c mytestdb
GRANT ALL ON monkeytest TO alison;

psql -U alison -d mytestdb
mytestdb=> SELECT * FROM monkeytest;


6) Connect to your database

Real port is 5432, but in this case I've set up a port forward (from 7732 -> 5432) to a local virtual machine using VirtualBox
psql -h localhost -p 7732 postgres
another example
psql -h localhost -p 7732 -U alison -d mytestdb

7) Backup that worked

pg_dump -Upostgres --format=c --file=testdb_backup.sqlc testdb
pg_restore -Upostgres --verbose --format=c testdb_backup.sqlc


=======

References
https://help.ubuntu.com/community/PostgreSQL

Comments

Popular posts from this blog

Switching from Docker Desktop to Rancher Desktop on WSL2: Solving the Missing docker-credential-desktop.exe Error

Running LXD/LXC on WSL2 with Ubuntu 20.04

Proxmox Remote administration via VNC