LIST USERS
POSTGRESQL

Postgres List Users: Two Different Approaches 

intro

A PostgreSQL server can have several users, and there are different ways to list them all. Explore three different approaches to list users in Postgres.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
PostgreSQL logo POSTGRESQL
THE POSTGRESQL DATABASE

Keeping track of users is a common task that database administrators must frequently perform. For PostgreSQL, there are a couple of Postgres list users procedures available, and the best one depends on your experience and the specific use case.

In this article, you will explore two different approaches to getting the list of users in PostgreSQL:

  • With a command-line command
  • With a query

Let’s learn how to list users in Postgres!

List All Users in Postgres With psql

psql stands for "PostgreSQL interactive terminal” and is a tool for interacting with a PostgreSQL server via the command line. In detail, it allows you to:

  • Add databases, tables, and users.
  • Run SQL queries in  the database.
  • Retrieve metadata information about the database objects.

Now that you know what psql is, follow the steps below to run the Postgres list users \du command:

  1. Open the terminal.
  2. Connect to your PostgreSQL server with the following psql command:

psql -U <username>

Replace <username> with the username of the PostgreSQL user you want to log in with. For example, replace it with the “postgres” username. After that, psql will ask you to enter the user's password. Type in it and press Enter to log into the server.

  1. List all the users with:

\du

This command will produce a table containing the users in the PostgreSQL server:

The table is returned by the "\du" command.
The table returned by the "\du" command.

Use \du+ to get additional information about each user, such as their description:

The table returned by the "\du+" command.
The table returned by the "\du+" command. Note the "Description" column.

Now for the second way - we will use a SQL query to list the users in our database instances.

List Users in Postgres With a Query

The pg_user view of the pg_catalog schema under the postgres default database provides access to information about database users. 

Launch the query below in a client supporting PostgreSQL, such as DbVisualizer, to get the list of all the users:

Copy
        
1 SELECT * FROM pg_catalog.pg_user;

The above Postgres list users query will return the following result:

Listing users in DbVisualizer.
Listing users in DbVisualizer.

Here’s what information each column of pg_catalog.pg_user contains:

  • usename: The name of the user.
  • usesysid: The number used by PostgreSQL to uniquely identify the user.
  • usecreatedb: true if the user has the CREATEDB role attribute, false otherwise.
  • usesuper: true if the user is a superuser, false otherwise.
  • userepl: true if the user has the permissions to launch streaming replication, false otherwise.
  • passwd: A placeholder for the user password.
  • valuntil: The expiry time of the user password.
  • useconfig: Session defaults for run-time configuration variables.

Congrats! You just saw two effective ways to list users in Postgres.

Conclusion

In this article, you saw two different Postgres list users methods. As seen here, getting the list of users in a PostgreSQL server is a simple task. There are two methods to achieve that, and here we dug into both of them. The first involves a command-line command in psql, while the second requires a specific query. 

To avoid using the CLI and better understand the result of your queries, you should adopt a powerful database client like DbVisualizer. This tool supports dozens of databases and comes with complete query optimization capabilities, a drag-and-drop UI to build queries, and ER schema representation functionality. Download DbVisualizer for free!

FAQ

How to list superusers in Postgres?

To list superusers in Postgres, you can query the pg_roles catalog table with a specific filter as follows:

Copy
        
1 SELECT * 2 FROM pg_catalog.pg_user 3 4 WHERE usesuper = true;

This will return only users with the SUPERUSER attribute.

How to identify and list the currently logged-in users in a PostgreSQL database?

To identify and list the currently logged-in users in PostgreSQL, run the query below on the pg_catalog.pg_stat_activity system view:

Copy
        
1 SELECT * 2 FROM pg_catalog.pg_stat_activity 3 WHERE state = 'active';
Identify and list the currently logged-in users.
The logged in user is the one used by DbVisualizer to connect to the database.

What role does authentication play when listing users in PostgreSQL?

Authentication ensures security by verifying the identity of the user attempting to access user-related information. Thus, by enforcing authentication PostgreSQL restricts unauthorized access, contributing to secure user listing.

Is listing a large number of Postgres users fast?

The speed of listing numerous PostgreSQL users depends on various factors, including server performance, database size, and hardware resources. At the same time, it can be considered a fast operation as it involves a simple query on a system view. 

What tools make it easier to list all users in Postgres?

A database client such as DbVisualizer provides a visual interface for executing queries. This tool offers SQL query execution capabilities, allowing you to run the "Postgres list users" query easily. In addition, it offers export functions, SQL history, and more to improve your PostgreSQL database management experience.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL CASE Statement: Definitive Guide

author Antonello Zanini tags CASE ORACLE POSTGRESQL SQL SQL SERVER 5 min 2024-04-29
title

SQL ORDER BY Clause: Definitive Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2024-04-22
title

Adding Dates in SQL: A Complete Tutorial

author Antonello Zanini tags DATE DATETIME MySQL POSTGRESQL SQL SERVER 7 min 2024-04-15
title

Glossary of the SQL Commands You Need to Know

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 12 min 2024-04-11
title

SQL Add to Date Operations: A Complete Guide

author Antonello Zanini tags DATE DATETIME MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2024-04-01
title

SQL CAST Function: Everything You Need to Know

author Antonello Zanini tags CAST MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2024-03-28
title

PostgreSQL Upsert: INSERT ON CONFLICT Guide

author Antonello Zanini tags POSTGRESQL UPSERT 7 min 2024-03-25
title

Postgres TEXT vs VARCHAR: Comparing String Data Types

author Antonello Zanini tags POSTGRESQL TEXT VARCHAR 6 min 2024-03-14
title

Schemas in PostgreSQL

author Leslie S. Gyamfi tags POSTGRESQL SCHEMA 6 min 2024-03-11
title

5 Ways to Split a String in PostgreSQL

author Antonello Zanini tags POSTGRESQL Split 6 min 2024-02-29
Read more from theTable

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind.  Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗

两个鬼故事微商起个好名字童装公司起名字大全水饺加盟川菜店铺起名i5-4200utokyohotn0769赵姓的宝宝起名字晋灵公五行缺木的公司起名生命的重建游戏加加全职法师小说免费阅读狗的寿命一般多长小孩起名网取名北新建材集团工作中存在的问题给服装店起名午夜寂寞影院支持安卓手机起名字更名字大全键盘连点智能起名崔氏男宝起名迪迪影院星河贵族缺火又缺水女孩起名李白写的诗辣手回春武汉科技大学城市学院贴吧农产品公司注册起名公共选择理论少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化