PostgreSQL Create User

We can create user in PostgreSQL by using createuser program or by using create user statement. To create user in PostgreSQL server, user must have create user(createrole) privileges.

Syntax to Create User in PostgreSQL:

CREATE USER name [ [ WITH ] option [ ... ] ] where option can be: SYSID uid | CREATEDB | NOCREATEDB | CREATEUSER | NOCREATEUSER | IN GROUP groupname [, ...] | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password' | VALID UNTIL 'abstime'

PostgreSQL Create User Examples:

1. Create user with a password.

CREATE USER johnd WITH PASSWORD 'John@432'; CREATE USER georgem WITH PASSWORD 'george@432';

2. Create a user with no password

CREATE USER mike;

3. Create an account where the user can create users:

CREATE USER vijay WITH PASSWORD 'vijay$321' createrole;

4. Create an account where the user can create databases:

CREATE USER kalpesh WITH PASSWORD 'kalpesh@321' CREATEDB;

5. To verify the list of users, use \du meta-command or run the query against table pg_user.