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.
Before you begin, ensure that:
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.
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.
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.
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:
This method is perfect for beginners or users managing multiple databases visually.
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.
Depending on your workflow, you can choose any of the above methods:
Each approach makes it simple to manage PostgreSQL users securely and efficiently.
If you need any further assistance, please contact our sales team.