Mengenal postgresql

27 Jan

1. start, shutdown postgresql
[root@bb bb]# service postgresql start
Initializing database: [ OK ]
Starting postgresql service: [ OK ]
[root@bb bb]# chkconfig postgresql on
[root@bb bb]# su potsgres
su: user potsgres does not exist
[root@bb bb]# su – postgres

2. createdb, dropdb
-bash-3.1$ createdb test
CREATE DATABASE
-bash-3.1$ psql test
Welcome to psql 8.1.4, 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

test=# \q

-bash-3.1$ dropdb test
DROP DATABASE

3. createuser, dropuser
-bash-3.1$ createuser bb
Shall the new role be a superuser? (y/n) y
CREATE ROLE

-bash-3.1$ dropuser bb
DROP ROLE

4. createuser & createdb
-bash-3.1$ createuser bb -P
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) y
CREATE ROLE

-bash-3.1$ createdb bb
CREATE DATABASE
-bash-3.1$ exit
logout

[root@bb ~]# su – bb
[bb@bb ~]$ psql
Welcome to psql 8.1.4, 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

-bash-3.1$ createuser admin -P
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) y
CREATE ROLE

5. edit configuration
error : postgresql fatal: ident authentication failed for user

[bb@bb ~]$ su – root
Password:
[root@bb ~]# vi /var/lib/pgsql/data/pg_hba.conf
# “local” is for Unix domain socket connections only
local all all password
host all all 127.0.0.1/32 password
host all all 172.20.0.0/16 password
host all all ::1/128 trust

[root@bb ~]# vi /var/lib/pgsql/data/postgresql.conf
listen_addresses = ‘*’

[root@bb ~]# service postgresql restart
Stopping postgresql service: [ OK ]
Starting postgresql service: [ OK ]
[root@bb ~]# exit

6. general command in PostgreSQL
\l :List databases
\c database-name :List databases
\d :List tables in database
\d table-name :Describe table
select * from table-name :List table contents

[bb@bb ~]$ psql -U bb -d bb -W
Password for user bb:
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.
bb=# \l
List of databases
Name | Owner | Encoding
———-+———-+———-
bb | postgres | UTF8
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
test2 | bb | UTF8
(5 rows)

bb=# \c
Password:
You are now connected to database “bb” as user “bb”.
bb=# \d
No relations found.

7. select database
bb=# \c test2
Password for user bb:
You are now connected to database “test2”.
test2=# \d
List of relations
Schema | Name | Type | Owner
——–+——————-+———-+——-
public | barang | table | bb
public | barang_id_seq | sequence | bb

test2=# \q
[bb@bb ~]$

8. test connection
[admin@bb ~]$ psql -U admin -d test2 -W
Password for user admin:
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.
test2=# \q

9. test connection from ip
[admin@bb ~]$ psql -U admin -d test2 -h 172.20.88.88 -W
Password for user admin:
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.
test2=# \q

2 Responses to “Mengenal postgresql”

  1. panjol November 7, 2009 at 6:10 pm #

    waduh ramudeng mas baru nyobak ki…..biasanya pake mysql,,,bagusan mana ya mysql ma postregesql?

    • setijoagus November 9, 2009 at 4:24 am #

      sejujurnya saya paling senang mainan oracle, kl postgresql cuma selingan, wkk wkk…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: