PostgreSQL add or create a user account and grant permission for database

Q. How do I create a user account called tom and grant permission for database called jerry?

A. You need to use following commands.
=> adduser - UNIX/Linux adduser command to add a user to /etc/passwd file

=> 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 tom

Type the following commands to create a UNIX/Linux user called tom:
# adduser tom
# passwd tom

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
OR
$ psql -d template1 -U postgres
Output:

Welcome to psql 7.4.16, the PostgreSQL interactive terminal.

Type:  \\copyright for distribution terms
       \\h for help with SQL commands
       \\? for help on internal slash commands
       \\g or terminate with semicolon to execute query
       \\q to quit

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 tom WITH PASSWORD 'myPassword';

Step #5: Add a database called jerry

Type the following command (you need to type command highlighted with red color):
template1=# CREATE DATABASE jerry;
Now grant all privileges on database
template1=# GRANT ALL PRIVILEGES ON DATABASE jerry to tom;
Type \q to quit:
template1=# \q

Step #6: Test tom user login

In order to login as tom you need to type following commands. Login as tom or use su command:
$ su - tom
$ psql -d jerry -U tom

Output:

Welcome to psql 7.4.16, the PostgreSQL interactive terminal.

Type:  \\copyright for distribution terms
       \\h for help with SQL commands
       \\? for help on internal slash commands
       \\g or terminate with semicolon to execute query
       \\q to quit

jerry=> 
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 11 comments… read them below or add one }

1 Mike 01.24.08 at 1:19 am

How can I do these same steps but on Windows?

Many thanks,
Mike

2 windows 07.17.08 at 8:54 pm

Mike while running windows:

open up a command window and run:
format c:

that will get rid of your windows issue nicely. ;) Before you do that you might want to have a linux cd handy recommending ubuntu.

3 vishwas 08.05.08 at 9:41 am

it is works thank u for helping me

4 roger 12.01.08 at 9:51 pm

Works! Awesome!

5 Allan Anderson 01.12.09 at 10:53 pm

This is the first (right on instructions) I have ever had the privilege to use and the only ones I have ever used that are right on.
Where can I find more of these easy to use fully explained to the smallest detail instructions.
Awesome
Allan

6 Xav 02.18.09 at 9:24 am

Great help thanks, and excellent humor also. I wonder why Mike has not responded yet LOL…

7 semuta 02.28.09 at 12:40 am

Thank you for the concise commands.

8 Arteal 03.07.09 at 9:34 pm

This is great beginner’s help, thanks :) But Will be great to explain here how to setup pqsql user without creating UNIX user, because i seem this as not so great resolution to create UNIX user for every pgsql user. Anyway, thanks for help once more :)

9 cam 03.15.09 at 11:59 pm

Awesome worked a treat thanks a lot. :D

10 joshg 05.01.09 at 3:57 pm

Great, simple and concise. Thanks a lot.

11 AfroSoft 05.17.09 at 9:55 pm

@Arteal
This is where windows informations can come useful.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , ,

Previous post: How do I unzip multiple / many files under Linux?

Next post: Linux display or change a pre-login message – /etc/issue file