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=>
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 10/24/07



{ 11 comments… read them below or add one }
How can I do these same steps but on Windows?
Many thanks,
Mike
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.
it is works thank u for helping me
Works! Awesome!
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
Great help thanks, and excellent humor also. I wonder why Mike has not responded yet LOL…
Thank you for the concise commands.
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 :)
Awesome worked a treat thanks a lot. :D
Great, simple and concise. Thanks a lot.
@Arteal
This is where windows informations can come useful.