Home → Articles → How to Create a PostgreSQL User and Grant Full Database Permissions on Ubuntu 26.04

How to Create a PostgreSQL User and Grant Full Database Permissions on Ubuntu 26.04

07 May, 2026

Introduction

PostgreSQL manages database access through a role-based system where each role can own database objects and receive specific privileges. When deploying web applications, you need a dedicated database user with full control over its own database while preventing other users from accessing that data. This isolation mirrors the MySQL approach of granting ALL PRIVILEGES ON database_name.* TO 'username'@'host', but PostgreSQL uses a different syntax with ownership transfers and schema-level permission grants. Understanding these PostgreSQL-specific commands helps you secure multi-tenant applications, CMS platforms, and enterprise resource planning systems where data isolation is critical.

This guide shows you how to create a PostgreSQL user, create a database with that user as owner, and restrict access to prevent other users from connecting or modifying the database.

Prerequisites

Before you start:

Log In to PostgreSQL as Superuser

The postgres user is the default superuser created during PostgreSQL installation. You need this account to create new users and databases.

Your shell prompt changes to postgres=# indicating you are inside the PostgreSQL interactive terminal.

Create a New PostgreSQL User

Each application or service needs its own database user with a secure password. This user becomes the owner of the application database.

Press Q to exit the role listing.

Create a Database with the New User as Owner

Setting the database owner during creation transfers full control to that user immediately. The owner can then create schemas, tables, and grant privileges without additional superuser intervention.

Press Q to exit the database listing.

Connect to the New Database as Superuser

You need to connect to the newly created database to modify its schema permissions.

Your prompt changes to erp_database=# showing you are now inside the target database.

Revoke Public Schema Access and Grant to Specific User

Every PostgreSQL database has a public schema that allows all users to create objects by default. You must revoke these default privileges to achieve proper data isolation.

These commands ensure only john_doe can create, modify, or delete objects in the public schema.

Restrict Database Connections to the Specific User

Other users cannot connect to the database by default after installation, but revoking CONNECT explicitly enforces this restriction.

Now only john_doe and superusers like postgres can establish connections to erp_database.

Exit the PostgreSQL Connection

After completing the permission setup, exit the PostgreSQL prompt to return to your system shell.

Verify Access by Logging in as the New User

Test that john_doe can connect to the database and access its objects.

Your prompt changes to erp_database=> showing successful authentication and connection.

Test That Other Users Cannot Access the Database

Create a test user to verify that your restriction works correctly.

The connection fails as expected, confirming that only john_doe can access the database.

Back Up the Database as the Superuser

Regular backups should run using the superuser account to read all objects regardless of ownership.

The pg_dump utility creates a backup file containing all SQL commands needed to recreate the database. The -U postgres flag uses the superuser account to read all objects.

Understand Why This Permission Order Matters

Setting permissions before creating any tables ensures that all future objects belong to john_doe rather than postgres. If you create tables as postgres before applying restrictions, those tables become owned by postgres, and john_doe would lack modification rights. By creating the database with john_doe as owner and applying schema and connection restrictions first, any objects you create afterward inherit john_doe as their owner. This order guarantees full control remains with the designated database user and no other roles can modify the database or its public schema.

Compare with MySQL Privilege Model

If you have used MySQL, the PostgreSQL approach achieves the same result as the MySQL GRANT ALL PRIVILEGES syntax. In MySQL, you grant full database access using:

SQL
GRANT ALL PRIVILEGES ON erp_database.* TO 'john_doe'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

PostgreSQL replaces this with three distinct steps:

Both systems achieve database-level isolation, but PostgreSQL offers finer control through schema separation and ownership inheritance.

Conclusion

In this guide, you have created a PostgreSQL user and database on Ubuntu 26.04, assigned full database ownership, revoked public schema access, and restricted connections to a single user. You also tested the configuration by logging in as the new user and verified that other users cannot connect. Now that you understand PostgreSQL user and database isolation, consider applying this pattern to each application in your infrastructure.