Thursday, August 9, 2007

Add user account to postgreSQL database

step:1 Add user to unix/linux - debian system

First you have issue following command to add unix/linux user..

# adduser user

It will ask name, password and bla bla.........

You can change password by issuing command like this...

# passwd password

step:2 Becomming a super user...

# su - postgres

Then u can connect to the database..

# psql template1


# psql -d template1 -U postgres

step:3 Add user....

It creates user called "user" and password called "password"

template1=# CREATE USER user WITH PASSWORD 'password';

step:4 Add a database called testdb....

template1=# CREATE DATABASE testdb;

step:5 Grant privileges to that database...

template1=# GRANT ALL PRIVILEGES ON DATABASE testdb to user;

exit from psql..

template1=# \q

step:6 Try to connect database ....

Try to connect to the database "testdb" user "user"

# su - user
# psql -d testdb -U user


How to connect to postgreSQL database through the network (linux - debian system)

First you have start the postgreSQL server....

# /etc/init.d/postgresql start

step:1 Allow remote access to postgresql database....

First you open the file called /etc/postgresql/pg_hba.conf.

# vim /etc/postgresql/8.1/main/pg_hba.conf

Now append the following line.

host all all ip_address net_mask auth_method

"ip_address" means your ip. "net_mask" means net mask. "auth_method" means authentication method. There are few authentication methods. such as "password, trust, md5" like that...

host all all trust

When we add auth_method as 'trust', think as trust user. If we add as 'md5', have to issue password while connect to this machine.

more details on postgresql doc

then save and close

step:2 Allow TCP/IP communication

open the file called /etc/postgresql/8.1/main/postgresql.conf. (8.1 means version. It may be vary)

Uncomment "listen_addresses" option.

It's default value is "localhost" and change to '*'.

Finally restart the postgresql database server..

# /etc/init.d/postgresql restart

step:3 Try to connect from remote machine

# psql -h -U postgres d- test

-h means host
-U means user name
-d means database name