Login

How to Create a PostgreSQL User in 5 Easy Ways

In PostgreSQL, a user is essentially a role with login privileges. Creating users helps database administrators, developers, and web hosting users manage secure access and permissions within the database environment.

In this guide, we’ll walk you through five easy methods to create a PostgreSQL user — whether you prefer the command line, graphical interface, or automation scripts.

Prerequisites

Before you begin, ensure that:

  • PostgreSQL is installed and running on your system.
  • You have access to the Postgres superuser (usually postgres).
  • You can connect via the psql tool or pgAdmin interface.

Method 1: Create a PostgreSQL User Using psql Command Line

The psql command-line tool allows you to connect to your PostgreSQL server and execute SQL commands directly.

This method is ideal for users who are comfortable working with terminals.

Step 1: Login to PostgreSQL as the default user:

psql -U postgres

Step 2: Create a new PostgreSQL user:

CREATE USER yogesh WITH PASSWORD ‘securepass123’;

This command creates a new user named yogesh with a login password.

Method 2: Create a PostgreSQL User Inside an Existing Database Session

If you’re already connected to your PostgreSQL database, you can directly create a user and assign privileges without reconnecting.

CREATE USER demo WITH PASSWORD ‘securepass123’;

GRANT CONNECT ON DATABASE your_db TO demo;

This grants the new user demo access to your chosen database.

Method 3: Create a PostgreSQL User Using the createuser Command

The createuser utility simplifies user creation through command-line flags. It’s great for automation and quick setups.

To create a basic user:

createuser demo –pwprompt

To create a superuser with database creation privileges:

createuser demo –superuser –createdb

You’ll be prompted to set a secure password during the process.

Method 4: Create a PostgreSQL User via pgAdmin (Graphical Interface)

If you prefer a graphical interface, pgAdmin provides an easy way to create and manage PostgreSQL users without writing any SQL commands.

Steps to create a user in pgAdmin:

  1. Open pgAdmin and connect to your PostgreSQL server.
  2. Navigate to Login/Group Roles in the left-hand panel.
  3. Right-click → Create → Login/Group Role.
  4. Enter the username, password, and required privileges.
  5. Click Save to finish.

This method is perfect for beginners or users managing multiple databases visually.

Method 5: Create a PostgreSQL User Using an SQL Script

If you’re setting up multiple users or automating deployments, you can store user creation commands in a .sql file and execute it using psql.

Example SQL Script (create_user.sql):

CREATE USER demo WITH PASSWORD ‘securepass123’;

GRANT CONNECT ON DATABASE your_db TO demo;

Run the script:

psql -U postgres -f create_user.sql

This approach saves time during repetitive database setups and ensures consistency.

Conclusion

Depending on your workflow, you can choose any of the above methods:

  • Command-line for faster operations.
  • pgAdmin for a user-friendly GUI experience.
  • SQL scripts for automation and scalability.

Each approach makes it simple to manage PostgreSQL users securely and efficiently.

If you need any further assistance, please contact our sales team.

 

Pooja Patil

How to Create a PostgreSQL User in 5 Easy Ways
Table of Contents
    ×