A Developer's Guide to Listing Users in PostgreSQL

August 8, 2025 (4mo ago)

Jump to FAQs

As a developer or database administrator, one of the most fundamental tasks you'll perform is managing user access. Whether you're auditing permissions, setting up a new application user, or just trying to get a lay of the land, you need a quick and reliable way to list users in PostgreSQL.

In this guide, we'll cover the two primary methods to view all users in a PostgreSQL database: the psql meta-command for rapid terminal checks and a direct SQL query for more granular, programmatic access.

Understanding the Core Concept: It's All About Roles

Before we dive in, it's crucial to understand that PostgreSQL handles users and groups under a unified concept: roles. A "user" is simply a role that has been granted the LOGIN privilege. This is an elegant design that simplifies permission management. When we ask Postgres to list users, we are actually asking it to list roles and their attributes.

The Quick Method: Using the psql Meta-Command

For immediate, human-readable output directly in your terminal, the psql command-line interface is your best friend. The fastest way to get a list of all roles is with the \du meta-command.

  1. First, connect to your database using psql:

    psql -U your_superuser -d your_database
  2. Once connected, simply run the command:

    \du

You'll see a neatly formatted table listing all roles, their attributes (like Superuser or Create DB), and the roles they are members of.

                                     List of roles
  Role name      |                         Attributes                         |  Member of
-----------------+------------------------------------------------------------+-------------
 laravel_app     |                                                            | {}
 postgres        | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 rama            | Superuser, Create role, Create DB                          | {postgres}
 readonly_group  | Cannot login                                               | {}

From this output, we can quickly see that laravel_app and rama can log in (they don't have Cannot login), while readonly_group cannot.

The Detailed Approach: Querying pg_catalog.pg_roles

When you need to list users programmatically—for instance, in a custom admin panel built in Laravel or a Go microservice—querying the system catalog is the superior method. The pg_catalog.pg_roles view contains the authoritative data on every role in the database cluster.

A direct SQL query gives you precise control.

SELECT rolname, rolsuper, rolcreaterole, rolcreatedb, rolcanlogin
FROM pg_catalog.pg_roles;

This query is perfect for an application backend because it returns structured data. For example, to specifically list roles that are users (i.e., those who can log in), you can filter by the rolcanlogin boolean column.

SELECT rolname FROM pg_catalog.pg_roles WHERE rolcanlogin = true;

In a Laravel application, you could run this query easily using the DB facade to populate a user management dashboard, ensuring you only display actual users and not permission groups.

// app/Http/Controllers/Admin/PostgresUserController.php
namespace App\Http\Controllers\Admin;
 
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
 
class PostgresUserController
{
    public function index()
    {
        // This query is safe from SQL injection as it has no user input.
        $users = DB::select("SELECT rolname FROM pg_catalog.pg_roles WHERE rolcanlogin = true;");
 
        return view('admin.users.index', ['users' => $users]);
    }
}

Mastering both the \du command for quick checks and the pg_roles catalog for application logic will make your PostgreSQL user management tasks far more efficient.

Discuss this post:

Frequently Asked Questions

How do I list all users in PostgreSQL?

There are two main ways. From the psql command line, use the \du meta-command for a quick, readable list. For programmatic access or more detail, run the SQL query: SELECT rolname FROM pg_catalog.pg_roles;

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

A 'user' is a type of 'role'. In PostgreSQL, a role is an entity that can own database objects and have database privileges. A role that has the LOGIN privilege is considered a user.

How can I see the permissions for a specific user in psql?

You can use the \du command followed by the role name to filter the list and see the attributes and group memberships for that specific user. For example: \du my_user_name.

How do I see which users are currently connected to the database?

To see active connections, you can query the pg_stat_activity view. Run SELECT usename, client_addr, state FROM pg_stat_activity; to get a list of connected users, their IP addresses, and their current connection state.

How do I create a new user in PostgreSQL?

You can create a new user with login privileges using the CREATE USER command, which is an alias for CREATE ROLE rolename WITH LOGIN. A common example is: CREATE USER new_user WITH PASSWORD 'a_secure_password';