Sabtu, 21 Februari 2015

postgreSQL Dasar

Untuk belajar psql ini diasumsikan telah mengenal konsep basis data dan SQL.

Pada tutorial ini ujicoba menggunakan Ubuntu Linux 9.04 dan PostgreSQL 8.3 atau bisa juga versi di atasnya, dan cara instalasi PostgreSQL silakan merujuk cara dari komunitas Ubuntu, untuk beberapa distro selain Ubuntu mungkin sedikit berbeda.

Instalasi

subura@localhost:~$ sudo apt-get install postgresql

User dan Database Baru

Setelah sukses menginstalasi, coba membuat user baru yang memiliki privilege sebagai superuser, perintahnya sebagai berikut.

subura@localhost:~$ sudo -u postgres createuser --superuser $USER
subura@localhost:~$ sudo -u postgres psql

$USER itu biasanya nama username Login Ubuntu, tapi di tutorial ini dibuat user berbeda dengan user login, misal: foo

subura@localhost:~$ sudo -u postgres createuser --superuser foo
subura@localhost:~$ sudo -u postgres psql
[sudo] password for subura:
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

postgres=#

Berikan password pada user 'foo'

postgres=# \password foo
Enter new password:
Enter it again:

Sekarang quit dengan perintah \q untuk masuk ke terminal lagi, lalu agar lebih mudah, coba buat database baru dengan nama yang sama dengan user: foo

subura@localhost:~$ createdb foo

Coba masuk login melalui psql menuju database 'foo' dan user 'foo', ketik:

subura@localhost:~$ psql foo
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

foo=#

Untuk melihat list semua database, ketik:

foo=# \l
List of databases
Name | Owner | Encoding
-----------+----------+----------
foo | subura | UTF8
postgres | postgres | UTF8
sekolah | subura | UTF8
subura | subura | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8

(6 rows)

foo=#

OK, sukses!

Alternatif lain membuat user baru yang memiliki hak penuh atas suatu database seperti berikut ini:

subura@localhost:~$ sudo -u postgres createuser -D -A -P myuser
[sudo] password for subura:
Enter password for new role:
Enter it again:
Shall the new role be allowed to create more new roles? (y/n) y

Perintah -D menghendaki myuser tidak memiliki hak membuat database baru, -A tidak memiliki hak menambahkan user baru, dan -P akan memberikan pertanyaan password pada prompt untuk user myuser. Pertanyaan 'Shall the new role be allowed to create more new roles? (y/n)' bila jawaban y (yes), maka myuser bisa membuat role baru.

Buat database baru dengan user myuser:

subura@localhost:~$ sudo -u postgres createdb -O myuser mydb

Bila berhasil, coba masuk via psql

subura@localhost:~$ psql mydb
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

mydb=#

Lihat list database, apakah mydb dan user myuser sudah ada dalam list

mydb=#\l
List of databases
Name | Owner | Encoding
-----------+----------+----------
foo | subura | UTF8
mydb | myuser | UTF8
postgres | postgres | UTF8
sekolah | subura | UTF8
subura | subura | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
(7 rows)

mydb=#

Ok ternyata ada, sukses lagi, berikan perintah SELECT untuk mengetahui psql mendengarkan perintah ini.

mydb=# select version();

version

----------------------------------------------------------------------------------------------------

PostgreSQL 8.3.10 on i486-pc-linux-gnu, compiled by GCC gcc-4.3.real (Ubuntu 4.3.3-5ubuntu4) 4.3.3

(1 row)


Yup, coba lagi:

mydb=# select current_date;

date

------------

2010-06-23

(1 row)

mydb=# select 10 * 10;

?column?

----------

100

(1 row)


Mantap, sampai di sini dulu. ;-)

Tidak ada komentar:

Posting Komentar