// SQL Server error reference

SQL Server error 15023: User, group, or role already exists

CREATE USER or a similar statement named a principal that already exists in the database, often an orphaned user left behind by a restore.

Error number
15023
Severity
16
Category
Permissions
Error message
User, group, or role '%s' already exists in the current database.

What it means

Database principal names are unique per database. Error 15023 appears when you create a user, role or group that is already there. After restoring a database to another server, the old database user is usually still present but no longer linked to a login, and trying to create it again fails with 15023.

Common causes

  • Running a setup script twice.
  • Restoring a database to a new server: the database user exists but is orphaned, because the new server's login has a different SID.
  • Mapping a login to a database in SSMS when a user of that name already exists.

How to fix it

  1. If the existing user is orphaned, link it to the login instead of creating a new one.

    ALTER USER app_user WITH LOGIN = app_user;
  2. Make setup scripts idempotent.

    IF DATABASE_PRINCIPAL_ID(N'app_user') IS NULL
        CREATE USER app_user FOR LOGIN app_user;

Related errors

More permissions errors

The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.

// fewer errors, faster fixes

Work on SQL Server in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.