How to Create Postgres User

Introduction How to create a Postgres user is a fundamental task in managing PostgreSQL databases effectively. PostgreSQL, commonly known as Postgres, is a powerful, open-source relational database system used widely for its reliability, extensibility, and standards compliance. Managing users within Postgres is crucial for controlling access, ensuring security, and maintaining database integrity.

Nov 17, 2025 - 10:56
Nov 17, 2025 - 10:56
 2

Introduction

How to create a Postgres user is a fundamental task in managing PostgreSQL databases effectively. PostgreSQL, commonly known as Postgres, is a powerful, open-source relational database system used widely for its reliability, extensibility, and standards compliance. Managing users within Postgres is crucial for controlling access, ensuring security, and maintaining database integrity.

Creating users with the appropriate privileges allows database administrators to safeguard sensitive data, delegate responsibilities, and optimize database operations. This tutorial provides a comprehensive guide on how to create Postgres users, highlighting practical steps, best practices, useful tools, real-world examples, and frequently asked questions.

Step-by-Step Guide

1. Accessing the PostgreSQL Database

Before creating a user, you need to connect to your PostgreSQL server. You can access the database using the psql command-line interface or a graphical client like pgAdmin.

To connect via terminal, open your command line and enter:

psql -U postgres

This command connects you to the Postgres database as the default superuser.

2. Understanding User Roles in PostgreSQL

In PostgreSQL, users are actually roles with login privileges. Roles can own database objects and have database privileges. The term role is more general, encompassing both users and groups.

3. Creating a Basic User

To create a new user with login capabilities, use the following SQL command:

CREATE USER username WITH PASSWORD 'password';

Replace username with the desired username and password with a secure password. For example:

CREATE USER alice WITH PASSWORD 'StrongPass123!';

4. Creating a User with Specific Privileges

By default, a new user has no privileges. Grant privileges explicitly to allow database access or administration.

Granting access to a specific database:

GRANT CONNECT ON DATABASE dbname TO username;

Granting usage on a schema:

GRANT USAGE ON SCHEMA public TO username;

Granting table privileges (SELECT, INSERT, UPDATE, DELETE):

GRANT SELECT, INSERT ON ALL TABLES IN SCHEMA public TO username;

5. Creating a Superuser

Superusers have full control over the PostgreSQL instance. To create a superuser:

CREATE USER admin WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'AdminPass!';

Be cautious when assigning superuser privileges as it allows unrestricted access.

6. Creating a User with Role Attributes

Postgres supports several role attributes. Here are some common examples:

  • CREATEDB: Allows the role to create new databases.
  • CREATEROLE: Allows the role to create, alter, and drop other roles.
  • LOGIN: Allows the role to log in (necessary for user accounts).
  • NOINHERIT: Prevents the role from inheriting privileges granted to roles it is a member of.

Example:

CREATE USER bob WITH LOGIN CREATEDB PASSWORD 'BobPass2024';

7. Modifying an Existing User

To change user attributes or passwords, use the ALTER USER command:

ALTER USER username WITH PASSWORD 'newpassword';

Example:

ALTER USER alice WITH PASSWORD 'NewStrongPass456!';

8. Deleting a User

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

DROP USER username;

Ensure that the user does not own any database objects before dropping, or reassign ownership beforehand.

9. Verifying Users and Roles

To list all users (roles with login permission), run:

\du

This psql meta-command displays roles, attributes, and membership.

Best Practices

1. Use Strong, Unique Passwords

Always assign complex passwords to Postgres users to protect against unauthorized access. Consider using password managers to generate and store credentials securely.

2. Least Privilege Principle

Grant users only the minimum privileges they need to perform their tasks. Avoid assigning superuser or elevated privileges unless necessary.

3. Separate Roles for Different Purposes

Create distinct roles for application access, administration, and reporting. This separation improves management and security.

4. Regularly Review User Privileges

Conduct periodic audits of user roles and permissions to identify and revoke unnecessary access.

5. Use Role Membership for Group Privileges

Instead of assigning privileges to individual users, create roles representing groups and assign privileges to those roles. Add users to roles as needed.

6. Avoid Using the Default postgres User for Applications

Create application-specific users to isolate applications and improve security.

7. Use SSL Connections for Remote Access

Encrypt connections to your Postgres server to protect credentials and data in transit.

Tools and Resources

1. psql PostgreSQL Command Line Interface

psql is the built-in interactive terminal for managing PostgreSQL databases, creating users, and running SQL commands.

2. pgAdmin

pgAdmin is a popular open-source graphical administration tool that allows you to manage users and roles visually.

3. PostgreSQL Documentation

The official PostgreSQL User Management Documentation is a comprehensive resource for understanding roles and permissions.

4. Security Best Practices Guides

Various online resources and security blogs provide best practices for Postgres security and user management.

5. Third-Party Management Tools

Tools like DBeaver and DataGrip support user management and provide advanced database administration features.

Real Examples

Example 1: Creating a Basic User with Login

Suppose you want to create a user named reporter with password Report2024! who can only read data:

CREATE USER reporter WITH LOGIN PASSWORD 'Report2024!';

Grant SELECT privileges on all tables in the public schema:

GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporter;

Example 2: Creating an Admin User

Create a superuser named dbadmin:

CREATE USER dbadmin WITH SUPERUSER LOGIN PASSWORD 'AdminSecure

1';

Example 3: Creating a User with Database Creation Rights

Create a user that can create new databases but is not a superuser:

CREATE USER devuser WITH LOGIN CREATEDB PASSWORD 'DevPass2024!';

Example 4: Modifying User Password

Change the password for user reporter:

ALTER USER reporter WITH PASSWORD 'NewReportPass!';

Example 5: Assigning Role Membership

Create a group role called developers and add user devuser to it:

CREATE ROLE developers;

GRANT developers TO devuser;

FAQs

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

PostgreSQL treats users as roles with login privileges. A role can represent a user or a group. Users are roles that can authenticate and log in.

Q2: Can a user have multiple passwords?

No, each user has a single password that is used for authentication.

Q3: How do I reset a PostgreSQL user password?

Use the ALTER USER username WITH PASSWORD 'newpassword'; command to reset the password.

Q4: Is it safe to use the default postgres user for applications?

No. It is best practice to create dedicated users with limited privileges for applications to improve security.

Q5: How do I check which users exist in my PostgreSQL server?

Use the \du command in psql to list all roles and users.

Q6: Can I create a user without a password?

Yes, but it is not recommended as it can lead to security vulnerabilities.

Q7: How do I grant a user permissions on a specific database?

Use GRANT CONNECT ON DATABASE dbname TO username; and then grant schema or table privileges as needed.

Conclusion

Creating and managing PostgreSQL users is a critical part of database administration that ensures secure and organized access control. By following the detailed steps outlined in this guide, database administrators can create users with appropriate privileges, enhance security, and streamline database operations.

Remember to adhere to best practices such as using strong passwords, granting least privileges, and regularly reviewing user roles. Utilize available tools like psql and pgAdmin to simplify user management. With proper user creation and management strategies, PostgreSQL databases can maintain integrity, security, and optimal performance.