Monday, April 18, 2011

PostgreSQL - Create a user, a database and grant accesses

These steps will let you create a user, a database (DB) and grant full access to the user to this DB.

All the commands are executed as the "postgres" privileged user.

For this, you use the command createuser which is provides with the postgreSQL package.
postgres@hostname:~$ createuser
Enter name of role to add: user
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE
postgres@hostname:~$

Create the DB

postgres@hostname:~$ createdb testdb
CREATE DATABASE
postgres@hostname:~$

Grand access for the user to the DB

postgres@hostname:~$ psql
postgres=# alter user user with encrypted password 'password';
ALTER ROLE
postgres=# grant all privileges on database testdb to user;
GRANT
postgres@hostname:~$ 
Hope it may work for you. If you have any issues please let me know.

1 comment: