PostgreSQL – How to Create a User

To create a new user in PostgreSQL, you can use the CREATE USER SQL command. Below is the basic syntax:

CREATE USER username [WITH PASSWORD 'password'];

where username is the desired username for the new user, and [WITH PASSWORD 'password'] is optional. If you include the password, the user will be created with that password. If you omit the password, the user will be created without a password, and they won’t be able to log in until a password is set.

Here’s an example of creating a user named “john” with a password:

CREATE USER john WITH PASSWORD 'secretpassword';

After creating the user, you might want to assign specific privileges or roles to the user using the ALTER USER command. For example, granting the ability to create databases and connect to existing ones:

ALTER USER john CREATEDB;

And if you want to allow the user to create more users:

ALTER USER john CREATEROLE;

Similar Posts