Berikut langkah instalasi PostgreSQL

Langkah 1 :

# apt-get update
# apt-get upgrade

Langkah 2 :

[email protected]:/home/mdp# apt-get install postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  linux-image-3.16.0-5-amd64
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
  postgresql-9.6 postgresql-client-9.6 postgresql-client-common postgresql-common postgresql-contrib-9.6 ssl-cert sysstat
Suggested packages:
  postgresql-doc locales-all postgresql-doc-9.6 libdbd-pg-perl openssl-blacklist isag
The following NEW packages will be installed:
  postgresql postgresql-9.6 postgresql-client-9.6 postgresql-client-common postgresql-common postgresql-contrib-9.6 ssl-cert sysstat
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,858 kB of archives.
After this operation, 31.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

Langkah 3 :

Agar service postgresql berjalan secara otomatis lakukan perintah ini :

# systemctl start postgresql

Langkah 4 :

Pastikan port 5432 listen menunjukkan postgresql berjalan :

[email protected]:/home/mdp# netstat -tapn | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      15521/postgres      
tcp6       0      0 ::1:5432                :::*                    LISTEN      15521/postgres

Langkah 5 :

Start postgresql

# /etc/init.d/postgresql start
[ ok ] Starting postgresql (via systemctl): postgresql.service.

Langkah 6 :

Stop postgresql

# /etc/init.d/postgresql stop
[ ok ] Stopping postgresql (via systemctl): postgresql.service.

Langkah 7 :

# /etc/init.d/postgresql restart
[ ok ] Restarting postgresql (via systemctl): postgresql.service.

Langkah 8 :

Pasang password postgresql :

# passwd postgres
$ su - postgres
Password: 
[email protected]:~$ psql
psql (9.6.10)
Type "help" for help.

postgres=#

Langkah 9 :

cara exit

postgres-# \q

Langkah 10 :

Cara membuat user posgresql

[email protected]:~$ su - postgres
Password: 
[email protected]:~$ create --pwprompt --interactive molavi
-su: create: command not found
[email protected]:~$ createuser --pwprompt --interactive molavi
Enter password for new role: 
Enter it again: 
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n

List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 molavi    | Create DB                                                  | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Langkah 11 :

Membuat database molavi

sintaks = createdb -O userNameHere dbNameHere

[email protected]:~$ su - postgres
Password: 
[email protected]:~$ createdb -O molavi dosen
[email protected]:~$ su - molavi
Password: 
[email protected]:~$ psql -U molavi -d dosen
psql (9.6.10)
Type "help" for help.

dosen=> 
[email protected]:~$ su - molavi
Password: 
[email protected]:~$ psql -U molavi -d dosen
psql (9.6.10)
Type "help" for help.

dosen=> CREATE TABLE foo(id int, name varchar);
CREATE TABLE
dosen=> INSERT INTO foo VALUES (1, 'Gita');
INSERT 0 1
dosen=> INSERT INTO foo VALUES (2, 'Mola');
INSERT 0 1
dosen=> INSERT INTO foo VALUES (3, 'MDP');
INSERT 0 1
dosen=> SELECT * FROM foo;
 id | name 
----+------
  1 | Gita
  2 | Mola
  3 | MDP
(3 rows)

dosen=> 

Langkah 12 :

Melisting tabel

dosen=> \d
       List of relations
 Schema | Name | Type  | Owner  
--------+------+-------+--------
 public | foo  | table | molavi
(1 row)

Langkah 13 :

Melihat database :

                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 dosen     | molavi   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)