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

by Vivek Gite on March 6, 2007 · 26 comments

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:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 26 comments… read them below or add one }

1 Mike January 24, 2008

How can I do these same steps but on Windows?

Many thanks,
Mike

Reply

2 parth January 3, 2011

write the same commands in cmd or just use “pgadmin” to do this stuff

Njoy :)

Reply

3 windows July 17, 2008

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.

Reply

4 Josh M. February 19, 2011

Funny thing is you wouldn’t even need instructions to do this stuff in Windows. But in a cryptic Linux OS nothing is obvious.

Reply

5 David September 19, 2011

Cryptic is your brain !!!

Reply

6 vishwas August 5, 2008

it is works thank u for helping me

Reply

7 roger December 1, 2008

Works! Awesome!

Reply

8 Allan Anderson January 12, 2009

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

Reply

9 Xav February 18, 2009

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

Reply

10 semuta February 28, 2009

Thank you for the concise commands.

Reply

11 Arteal March 7, 2009

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 :)

Reply

12 cam March 15, 2009

Awesome worked a treat thanks a lot. :D

Reply

13 joshg May 1, 2009

Great, simple and concise. Thanks a lot.

Reply

14 AfroSoft May 17, 2009

@Arteal
This is where windows informations can come useful.

Reply

15 SIFE March 13, 2010

@Mike
go to:
start->programs->postgreSQL 8.*->SQL shell
login with your postgres user and password then type this commands in this order :
CREATE USER win;
CREATE DATABASE test;
GRANT ALL PRIVILEGES ON DATABASE test to win;

change win to a defined user by you ,and test to a database you want .

Reply

16 John July 29, 2010

And to undo:

REVOKE ALL ON DATABASE jerry FROM tom;
DROP USER tom
DROP DATABASE jerry

Reply

17 drone115b August 20, 2010

So how do you query the permissions a user has? It seems naturaly that, after changing these settings a careful admin will want to verify them.

Reply

18 gnubala September 7, 2010

i need help
i need to see how many users have created. is it possible?
is any command is there for listing the users in postgres?

Reply

19 lakshmi September 13, 2010

this information is gud but their should be continuation to another relevant topic

Reply

20 gnubala September 14, 2010

using \du command we can see the list of users which we have created

Reply

21 Jaysunn March 5, 2011

Vivek,
I used this great quick tutorial to get me out of a place that I was unfamiliar with. Thanks!!!

Question 1. what the heck does postgressql mean, the name?
I see post, but gre? Maybe you know?

jaysunn

Reply

22 Vivek Gite March 7, 2011

Jaysunn,

PostgreSQL[1] evolved from the Ingres project at the University of California, Berkeley. In 1982, the project leader, Michael Stonebraker, left Berkeley to make a proprietary version of Ingres. He returned to Berkeley in 1985 and started a post-Ingres project to address the problems with contemporary database systems that had become increasingly clear during the early 1980s. The new project, Postgres, aimed to add the fewest features needed to completely support types. These features included the ability to define types and to fully describe relationships – something used widely before but maintained entirely by the user. In Postgres, the database “understood” relationships, and could retrieve information in related tables in a natural way using rules. Postgres used many of the ideas of Ingres, but not its code.

[1] Source: Wikipedia

HTH

Reply

23 Jeff March 9, 2011

Great instructions. Thank you very much! On to figure out how to login to the database via PHP…

Reply

24 suhail March 11, 2011

just wanted to add

to create with UTF8 :-

CREATE DATABASE example WITH ENCODING ‘UTF8′;

Reply

25 JACOB OBINGUAR March 28, 2011

Hi! I am new to PostgreSQL. This page has so much help! Thanks a lot!!! Just one more question, how do give additional roles to the user you have created like (create role, create db etc..)? Once again, thanks!

Reply

26 Ralph Siegler September 13, 2011

Nonsense, GNU/Linux admin is much easier than Windows. I speak as former Windows and Metaframe admin. The Windows admins at my employer spend more time by a factor of three to admin each windows box as I do with all the Linux boxes together. The command line admin of postgresql in windows is the same, or you can use GUI tools to admin it the same way in Windows as Linux. What you are really saying is you only want to stay with one way of doing things without learning any better way. Windows is totally crippled and featureless compared to what GNU/Linux can do, and is slower and requires twice the RAM for a given job. And to show my objectiveness, there are even better OS than Linux, the BSD for example like FreeBSD or OpenBSD are of even higher quality, or OpenSolaris. All those can also run Postgresql, though FreeBSD would probably be the fastest.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the simple math so we know that you are a human and not a script.




Previous post:

Next post: