A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/PostgreSQL/snippets/postgresql-show-users.php below:

Website Navigation


Listing all users and roles in PostgreSQL

Listing all users and roles in PostgreSQLLast update on December 23 2024 07:42:12 (UTC/GMT +8 hours)

PostgreSQL: Show Users in a Database

In PostgreSQL, users (also called roles) have specific privileges and permissions. Displaying a list of all users is often necessary for database administration, to review access control, and manage permissions across the system. This guide covers how to list all users in PostgreSQL, detailing the syntax and useful examples.

Syntax:

To show all users in a PostgreSQL database, you can use the following queries:

1. Using \du Command in psql

The \du command is a quick way to display all users and their roles directly from the psql command-line interface.

\du

2. Querying the pg_roles System Catalog

Another method is to query the pg_roles system catalog, which stores information about all roles (users and groups) in PostgreSQL.

SELECT rolname FROM pg_roles;

Example 1: Displaying Users with the \du Command

Code:

-- Lists all users and roles in the PostgreSQL database with associated attributes
\du

Explanation:

Example 2: Displaying Users with a Query on pg_roles

Code:

-- Retrieves a list of all usernames (roles) from the pg_roles system catalog
SELECT rolname FROM pg_roles;

Explanation:

Example 3: Showing Additional Details for Users

Code:

-- Retrieves user details, including superuser and login privileges
SELECT 
    rolname,          -- Username
    rolsuper,         -- Superuser status (true/false)
    rolcreaterole,    -- Role creation privileges (true/false)
    rolcreatedb,      -- Database creation privileges (true/false)
    rolcanlogin       -- Login capability (true/false)
FROM pg_roles;

Explanation:

Important Notes:

1. Difference Between Roles and Users:

2. Granting and Modifying User Permissions:

Code:

ALTER ROLE username WITH CREATEDB;

This command grants the user username permission to create databases.

3. Superuser Privileges:

All PostgreSQL Questions, Answers, and Code Snippets Collection.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4