UID (User ID)

The UID is the number Drupal uses to identify a user account. It appears in addresses like /user/42, and 0 and 1 are reserved for special accounts.

Learn DrupalDrupal glossary

A UID, short for user ID, is the number Drupal assigns to each user account. It is the identifier of the user entity, stored in the uid column, and it shows up in the account's address, such as /user/42. Two numbers have a fixed meaning: 0 is the anonymous visitor, and 1 is the account created when the site was installed.

How it works in Drupal 11

The user entity is declared in Drupal\user\Entity\User. Its entity keys map id to uid, and it has a uuid key as well. The data lives in the users and users_field_data tables. The entity defines these paths; in the first three, the placeholder stands for the UID:

  • /user/{user}: the profile page.
  • /user/{user}/edit: the account edit form.
  • /user/{user}/cancel: the cancel form.
  • /admin/people: the People list.

Anything that implements AccountInterface returns its UID from id(), with 0 for an anonymous account. The method isAuthenticated() on a user is true when the ID is greater than 0, and isAnonymous() covers 0. The installer, in user_install(), creates the row for UID 0 and then a row for UID 1 that the install form fills in. UID 1 has extra power by default, explained under user 1. UID 0 is explained under anonymous.

Other data points back to the UID. The node entity uses a uid key as its owner, so every node carries the number of the account that owns it. The sibling identifier for content is the NID.

Example: find and use a UID

For account number 42, the profile is at /user/42 and the edit form at /user/42/edit. On the command line, Drush prints the account details, including uid, name, mail, status and roles:

drush user:information --uid=42

In custom code, get the UID of the person making the request from the current user:

$uid = \Drupal::currentUser()->id();
        $account = \Drupal\user\Entity\User::load($uid);

The Drupal API documentation shows this same load pattern for turning the current user into a full user entity.

Common mistakes

  • Hard-coding a UID other than 0 or 1. The number is an auto-increment value from one site's database. Look the account up by name or email, or use its UUID, which is meant to identify an entity across systems, when code or configuration moves between environments.
  • Testing the number by hand. Core's own isAnonymous() check accepts both the integer 0 and the string '0'. Call the method instead of comparing with ===.
  • Treating the username as the key. The username is a label; references between records use the UID.
  • Mixing up UID and NID. /user/5 and /node/5 are unrelated; the number in each address belongs to a different entity type.

For marketing and content teams

You will meet the UID when a developer asks "which account?" Open the People list, click the person and read the number in the address bar. It also helps when you report an author problem: "content written by user 42" is unambiguous even when two people share a display name. The number is not a secret and not a ranking: UID 1 is the installer's account, not necessarily the most senior editor. For roles and what each person may do, see roles and permissions.

Sources

Questions about UID (User ID)

Open the People list at /admin/people and click the account. The number in the address, as in /user/42, is the UID. With Drush, drush user:information --uid=42 shows an account by number, and drush user:information jane finds it by name.

UID 0. Drupal uses it for every visitor who is not logged in, and the installer creates a blocked user row with that ID. The method isAnonymous() on an account tells you whether it applies.

The username is the name shown for the account. The UID is the number Drupal uses internally to point to it, for example in the address /user/42 and in the author field of a node. References between records use the number, not the display name.

Both identify a user, and both are entity keys of the user entity. The UID is an auto-increment number from the site's own database. The UUID is a universally unique identifier, and the Drupal API documentation says it can be used to identify an entity across multiple systems.

Related terms

All Drupal terms from A to Z