PostgreSQL : create a user, a database and grant accesses
stephane
The following step-by-step 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.
Create the user
For this, you use the command createuser which is provides with the postgreSQL package. Then answer the questions as you see fit :
postgres@hostname:~$ createuser<br /> Enter name of role to add: username<br /> Shall the new role be a superuser? (y/n) n<br /> Shall the new role be allowed to create databases? (y/n) n<br /> Shall the new role be allowed to create more new roles? (y/n) n<br /> CREATE ROLE<br /> postgres@hostname:~$<br />
Create the DB
Use the createdb command to create the database :
postgres@hostname:~$ createdb databasename<br /> CREATE DATABASE<br /> postgres@hostname:~$<br />
Grand access for the user to the DB
And last, using the psql command, set a password for the user and grant accesses :
postgres@hostname:~$ psql<br /> postgres=# alter user username with encrypted password 'password';<br /> ALTER ROLE<br /> postgres=# grant all privileges on database databasename to username;<br /> GRANT<br /> postgres@hostname:~$<br />