The following cheats apply to PostgreSQL 9.4 on CentOS (and also RHEL and standard Amazon Linux on EC2 instances).
sudo yum install postgresql94 postgresql94-docs postgresql94-server postgresql94-test postgresql94-contrib
sudo service postgresql94 initdb
The following allows unrestricted connections over the network. Make sure that you read documentation to understand what you really doing. And please, note, that THIS IS UNSAFE, use for dev purposes only.
sudo vim /var/lib/pgsql94/data/pg_hba.conf
then add
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 0.0.0.0/0 md5
host all all ::1/0 md5
then do
sudo vim /var/lib/pgsql94/data/postgresql.conf
and add
listen_address='*'
port = 5432
Then restart your postgresql process:
sudo service postgresql94 restart
Login as postgres
user, then do
sudo su - postgres
psql -U postgres
and then create users:
ALTER USER postgres WITH PASSWORD '$password1';
CREATE USER pwr_user SUPERUSER;
ALTER USER pwr_user WITH PASSWORD '$password2';
CREATE USER demo_user NOSUPERUSER;
ALTER USER demo_user WITH PASSWORD '$password3';
CREATE DATABASE test_db WITH OWNER demo_user;
Example (just demo user + demo DB):
CREATE USER demo_user NOSUPERUSER;
ALTER USER demo_user WITH PASSWORD 'foo';
CREATE DATABASE test_db WITH OWNER demo_user;
The following will connect to localhost, it wouldn't work unless you enabled network connections similarly to what shown above:
psql -h 127.0.0.1 -U $YOUR_USERNAME $YOUR_DB
# e.g.:
psql -h 127.0.0.1 -U demo_user test_db
Sample books table:
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title VARCHAR(1024) NOT NULL,
pages INTEGER
);
INSERT INTO books (id, title, pages) VALUES (1, 'Noon', 392);
INSERT INTO books (id, title, pages) VALUES (2, 'Far Rainbow', 504);
INSERT INTO books (id, title, pages) VALUES (3, 'Hermit and Sixfinger', 156);
INSERT INTO books (id, title, pages) VALUES (4, 'Throttle', 94);
INSERT INTO books (id, title, pages) VALUES (5, 'Gunpowder', 211);
INSERT INTO books (id, title, pages) VALUES (6, 'The Time Wanderers', 405);
INSERT INTO books (id, title, pages) VALUES (7, 'Swans', 65);
INSERT INTO books (id, title, pages) VALUES (8, 'Audition', 275);