Q. How do I create a user account called tom and grant permission for database called pio?
=> psql => It is a terminal-based front-end to PostgreSQL.
=> CREATE USER – Adds a new user to a PostgreSQL database cluster.
=> CREATE DATABASE – create a new database
=> GRANT ALL PRIVILEGES – define access privileges
Procedure to add a user to PostgreSQL database
To create a normal user and an associated database you need to type the following commands. The easiest way to use is to create a Linux / UNUX IDENT authentication i.e. add user tom to UNIX or Linux system first.
Step # 1: Add a Linux/UNIX user called dbsql
Type the following commands to create a UNIX/Linux user called tom:
# adduser dbsql
# passwd dbsql
Step # 2: Becoming a superuser
You need to login as database super user under postgresql server. Again the simplest way to connect as the postgres user is to change to the postgres unix user on the database server using su command as follows:
# su - postgres
Step #3: Now connect to database server
Type the following command
$ psql template1
$ psql -d template1 -U postgres
psql (9.2.18) Type "help" for help. template1=#
Step #4: Add a user called tom
Type the following command to create a user called tom with a password called myPassword (you need to type command highlighted with red color):
template1=# CREATE USER dbsql WITH PASSWORD 'myPassword';
Step #5: Add a database called pio
Type the following command (you need to type command highlighted with red color):
template1=# CREATE DATABASE pio;
Now grant all privileges on database
template1=# GRANT ALL PRIVILEGES ON DATABASE pio to dbsql;
Type \q to quit:
Step #6: Test dbsql user login
In order to login as tom you need to type following commands. Login as tom or use su command:
$ su - dbsql
$ psql -d pio -U dbsql
psql (9.2.18) Type "help" for help. pio=>