How to Create Postgres User

Introduction PostgreSQL, often simply called Postgres, is a powerful open-source relational database management system widely used for its robustness, extensibility, and standards compliance. One of the fundamental tasks in managing a Postgres database is creating users who can access and manipulate the database securely and efficiently. Understanding how to create Postgres users is essential for

Nov 17, 2025 - 11:16
Nov 17, 2025 - 11:16
 4

Introduction

PostgreSQL, often simply called Postgres, is a powerful open-source relational database management system widely used for its robustness, extensibility, and standards compliance. One of the fundamental tasks in managing a Postgres database is creating users who can access and manipulate the database securely and efficiently. Understanding how to create Postgres users is essential for database administrators, developers, and IT professionals to maintain database security, assign appropriate privileges, and ensure smooth operation of applications.

This tutorial will provide a comprehensive guide on how to create users in Postgres. We will explore the importance of user roles, the step-by-step process of creating users, best practices for managing user permissions, useful tools and resources, real-world examples, and answers to frequently asked questions. By the end of this guide, you will have a solid understanding of how to manage Postgres users effectively.

Step-by-Step Guide

1. Understanding Postgres Roles and Users

In PostgreSQL, the terms "roles" and "users" are often used interchangeably. A role can represent a single user or a group of users. Roles allow you to manage database access and privileges. When you create a role with login privileges, it acts as a user account.

2. Prerequisites

Before creating a Postgres user, ensure the following:

  • You have access to the PostgreSQL server with administrative privileges (usually the postgres user).
  • PostgreSQL is installed and running on your system.
  • You can connect to the database via psql or a suitable database client.

3. Connecting to the PostgreSQL Server

Open your terminal or command prompt and connect to your Postgres server using the superuser account:

psql -U postgres

If your server is remote, add the host parameter:

psql -U postgres -h your_host_address

4. Creating a Basic User

To create a simple user with login privileges, use the following SQL command:

CREATE USER username WITH PASSWORD 'password';

Example:

CREATE USER alice WITH PASSWORD 'securepassword123';

This command creates a user called alice with the specified password.

5. Creating a User with Additional Privileges

You can create users with different levels of privileges. Common options include:

  • SUPERUSER: Grants all administrative rights.
  • CREATEDB: Allows the user to create databases.
  • CREATEROLE: Allows the user to create, alter, and drop other roles.
  • LOGIN: Allows the role to log in (needed for user accounts).

Example:

CREATE USER bob WITH LOGIN CREATEDB PASSWORD 'strongpass';

This creates a user bob who can log in and create databases.

6. Creating a Role Without Login (Group Role)

Postgres allows you to create roles that act as groups to manage privileges:

CREATE ROLE developers;

You can then add users to this role:

GRANT developers TO alice;

Users inherit the privileges of roles they belong to.

7. Granting Privileges to Users

After creating users, you typically want to grant them access to certain databases or tables.

Example: Grant connect access to a database:

GRANT CONNECT ON DATABASE mydatabase TO alice;

Grant usage on a schema:

GRANT USAGE ON SCHEMA public TO alice;

Grant select permission on a table:

GRANT SELECT ON table_name TO alice;

8. Altering a User

You can modify user attributes using the ALTER USER command:

Change password:

ALTER USER alice WITH PASSWORD 'newpassword';

Grant superuser status:

ALTER USER alice WITH SUPERUSER;

9. Dropping a User

If a user is no longer needed, remove it safely:

DROP USER alice;

Ensure the user is not the owner of any database objects before dropping.

Best Practices

1. Use Roles for Group Management

Create roles without login to group users with similar access needs, simplifying privilege management.

2. Assign the Least Privilege

Grant users only the necessary permissions they need to perform their tasks to minimize security risks.

3. Use Strong Passwords

Ensure all user passwords are strong and follow organizational security policies.

4. Regularly Review and Audit Users

Periodically check user roles and privileges, removing unused users and adjusting permissions as needed.

5. Avoid Using Superuser Privileges Excessively

Reserve superuser rights for essential administrative tasks to reduce potential damage from compromised accounts.

6. Secure Connection Settings

Configure pg_hba.conf properly to control how users connect to the database server.

Tools and Resources

1. psql Command Line Interface

The native PostgreSQL command-line tool to run SQL commands and manage users.

2. pgAdmin

A popular graphical interface for managing PostgreSQL databases, including user roles and permissions.

3. DBeaver

A multi-database management tool that supports PostgreSQL and user management features.

4. Official PostgreSQL Documentation

CREATE USER Documentation The authoritative source for all PostgreSQL commands and best practices.

5. Online Tutorials and Courses

Various platforms offer in-depth PostgreSQL training covering user management and security.

Real Examples

Example 1: Creating a Read-Only User

Suppose you want to create a user who can only read data from the database without making changes.

Steps:

  1. Create the user:

CREATE USER readonly WITH PASSWORD 'readonlypass';

  1. Grant CONNECT to the database:

GRANT CONNECT ON DATABASE mydb TO readonly;

  1. Grant USAGE on the schema:

GRANT USAGE ON SCHEMA public TO readonly;

  1. Grant SELECT on all tables:

GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;

To ensure future tables are also accessible:

ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;

Example 2: Creating an Admin User with Database Creation Rights

This user can log in and create new databases but is not a superuser.

CREATE USER dbadmin WITH LOGIN CREATEDB PASSWORD 'adminpass';

Example 3: Creating a Group Role and Assigning Users

Create a role for developers:

CREATE ROLE developers;

Grant privileges to the developers role:

GRANT CONNECT ON DATABASE projectdb TO developers;

Add users to this group:

GRANT developers TO alice;

GRANT developers TO bob;

FAQs

Q1: What is the difference between a user and a role in PostgreSQL?

A role is a broader concept in PostgreSQL that can represent either a user or a group of users. A role with login privileges is effectively a user account.

Q2: Can I create users without passwords?

Yes, but it is highly discouraged for security reasons. Passwords protect access and are essential for authentication.

Q3: How do I revoke privileges from a user?

Use the REVOKE command. For example:

REVOKE SELECT ON table_name FROM alice;

Q4: How do I check existing users in PostgreSQL?

Run the following query:

SELECT rolname FROM pg_roles;

Q5: Can I assign multiple roles to a user?

Yes, a user can be a member of multiple roles. Each role grants different privileges.

Conclusion

Creating and managing users in PostgreSQL is a critical aspect of database administration that ensures secure and efficient access control. By following the steps outlined in this tutorial, you can confidently create users, assign appropriate privileges, and maintain a secure database environment. Adhering to best practices, such as applying the principle of least privilege, regularly auditing user access, and using roles for group management, further strengthens database security.

Leveraging tools like psql and pgAdmin can simplify user management tasks, while official documentation and community resources offer valuable guidance. Whether you are creating a read-only user, an admin with database creation rights, or managing groups of users, mastering Postgres user creation is foundational to effective database management.