`\dg` vs `\du` in PostgreSQL

202 views Asked by At

I showed users(roles) with \dg and \du, then there were the same results as shown below:

postgres=# \dg
                             List of roles
 Role name |                         Attributes
-----------+------------------------------------------------------------
 anna      |
 john      |
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS
postgres=# \du
                             List of roles
 Role name |                         Attributes
-----------+------------------------------------------------------------
 anna      |
 john      |
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS

I also read the explanations of \dg and \du which are the same as shown below:

postgres=# \?
...
Informational
  ...
  \dg[S+] [PATTERN]      list roles
  ...
  \du[S+] [PATTERN]      list roles
  ...

My questions:

  1. What is the difference between \dg and \du?
  2. If \dg and \du are the same, is one of them deprecated at the moment or in the future?
3

There are 3 answers

0
Zegarek On

Using psql --echo-hidden:

-E

--echo-hidden

Echo the actual queries generated by \d and other backslash commands. You can use this to study psql's internal operations. This is equivalent to setting the variable ECHO_HIDDEN to on.

You can see \dg and \du are exactly the same thing:

SELECT r.rolname, r.rolsuper, r.rolinherit,
  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
  r.rolconnlimit, r.rolvaliduntil,
  ARRAY(SELECT b.rolname
        FROM pg_catalog.pg_auth_members m
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
        WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
WHERE r.rolname !~ '^pg_'
ORDER BY 1;

Neither is deprecated. It's not guaranteed that they won't be replaced or deprecated in the future, but it's generally unlikely.

\dg lists groups, \du lists users, which both became roles in version 8.1.

0
Laurenz Albe On

The documentation says:

\dg[S+] [ pattern ]

Lists database roles. (Since the concepts of “users” and “groups” have been unified into “roles”, this command is now equivalent to \du.)

It does not matter which one you use. I cannot see into the future, but I would be surprised if either of these commands ever gets removed. The maintenance costs are low, and backward compatibility is valued in PostgreSQL.

0
Álvaro González On

These commands pre-date PostgreSQL/8.1, when users and groups were replaced with a single entity, roles:

The concept of roles subsumes the concepts of "users" and "groups". In PostgreSQL versions before 8.1, users and groups were distinct kinds of entities, but now there are only roles. Any role can act as a user, a group, or both.

(Source)

There's also a note in documentation for \dg says:

(Since the concepts of “users” and “groups” have been unified into “roles”, this command is now equivalent to \du.)