DEV Community

Cover image for Simplifying PostgreSQL User Management: Two Methods
DbVisualizer
DbVisualizer

Posted on

Simplifying PostgreSQL User Management: Two Methods

Managing database users is a core task for any administrator. PostgreSQL offers two straightforward methods to list users: via the psql terminal and by querying the database. This guide walks you through both approaches.

Listing users in PostgreSQL

Use the psql command

The psql terminal lets you directly manage and query your database.

Connect to PostgreSQL

Use this command to log in.

psql -U <username>
Enter fullscreen mode Exit fullscreen mode

Run the Command

Enter this command for a list of users.

\du
Enter fullscreen mode Exit fullscreen mode

Or this for detailed output:

\du+
Enter fullscreen mode Exit fullscreen mode

Query the database

For structured information, query the pg_user view.

Run the Query

SELECT * 
FROM pg_catalog.pg_user;
Enter fullscreen mode Exit fullscreen mode

Interpret the Results

This shows user details, including permissions and roles.

FAQ

How do I display superusers only?

Run this query

SELECT * 
FROM pg_catalog.pg_user 
WHERE usesuper = true;
Enter fullscreen mode Exit fullscreen mode

Can I see logged-in users?

Yes, query pg_stat_activity

SELECT * 
FROM pg_catalog.pg_stat_activity 
WHERE state = 'active';
Enter fullscreen mode Exit fullscreen mode

Why is authentication necessary?

It ensures security by restricting access to sensitive data and preventing unauthorized users from listing database users.

Which tools can simplify this process?

DbVisualizer offers a visual interface for managing queries and inspecting user details more easily.

Conclusion

PostgreSQL makes listing users simple with psql commands or SQL queries. Use these methods to maintain database security and organization. For more, visit the article Postgres List Users: Two Different Approaches.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay