SQL Server Engineering

Repair Orphaned SQL Server Users After a Database Restore

Diagnose login-to-user SID mismatches, remap the intended identity without dropping permissions, and validate application access after a restore.

A database restore can complete successfully while the application still cannot use its data. One common cause is an orphaned database user: the database contains a user mapped to a login SID that is not present on the destination instance. Recreating a login with the same visible name does not necessarily recreate the same SID.

Separate authentication from database authorization. A login establishes an identity at the instance. A database user represents that identity inside a particular database and carries memberships and permissions there. Restoring the database brings the user, but does not automatically bring every instance-level login and dependency.

Diagnose the mapping by SID

Run this read-only query in the restored database using an administrative diagnostic connection with enough visibility into server principals.

-- SQL-authenticated, instance-mapped users in the current database.
SELECT dp.name AS DatabaseUser, dp.sid AS DatabaseUserSid,
       dp.authentication_type_desc
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp ON sp.sid = dp.sid
WHERE dp.type = 'S'
  AND dp.authentication_type_desc = 'INSTANCE'
  AND dp.principal_id > 4
  AND sp.principal_id IS NULL
ORDER BY dp.name;

The query deliberately focuses on SQL-authenticated users mapped to instance logins. It is not a universal audit of Windows groups, contained users, certificate users, or external identities. A user created WITHOUT LOGIN is not broken merely because it has no server login.

Metadata visibility matters. A restricted diagnostic account may not see all server principals and can produce apparent orphans that are not actually missing. Verify the result under the approved administrative context before changing mappings.

Inspect the intended destination login separately, including its SID, enabled state, authentication type, and purpose. A matching name is a clue, not authorization to bind identities. Two environments may use the same account name for different services or levels of access.

Also distinguish this condition from a disabled login, an unavailable default database, an incorrect connection string, or missing CONNECT permission. Error 18456 alone is not a complete diagnosis. Use the server-side error details and an actual connection attempt to narrow the failing stage.

Remap the existing user

When the correct destination login already exists, remap the database user in place.

-- Run in the restored database only after verifying the intended mapping.
ALTER USER [AppUser] WITH LOGIN = [AppLogin];

AppUser and AppLogin are illustrative names that must be replaced with the verified pair. ALTER USER preserves the existing database principal rather than dropping and recreating it, so its database permissions and role relationships can remain attached.

Dropping the user as a shortcut may lose explicit permissions or fail because it owns schemas or other objects. Recreating it with an identical name does not guarantee that every previous relationship is restored. Do not turn a mapping repair into an accidental permission redesign.

If the login genuinely needs to be recreated, decide whether to preserve the original SID through an approved login-migration process. This can keep the mapping valid across multiple restored databases. Handle credentials and login policies through the established secure process, rather than copying passwords into an ad hoc repair script.

Avoid automated name-only remapping across every user. Review a concrete source-to-destination identity list, especially when consolidating databases from different instances. The same name can conceal a different trust boundary.

Validate both identity and capability

Confirm the SID relationship and inspect the user's expected role memberships.

SELECT dp.name AS DatabaseUser, sp.name AS MappedLogin,
       dp.sid AS DatabaseUserSid, sp.sid AS LoginSid
FROM sys.database_principals AS dp
JOIN sys.server_principals AS sp ON sp.sid = dp.sid
WHERE dp.name = N'AppUser';

SELECT r.name AS DatabaseRole
FROM sys.database_role_members AS drm
JOIN sys.database_principals AS r ON r.principal_id = drm.role_principal_id
JOIN sys.database_principals AS m ON m.principal_id = drm.member_principal_id
WHERE m.name = N'AppUser';

The role query is only one part of the permission inventory. Direct object grants, schema permissions, explicit denies, and ownership may also affect access. Compare the relevant permissions before and after the repair rather than assuming that role membership describes everything.

Then open a fresh connection using the application's actual login and initial database. Execute a representative allowed operation and verify that an operation outside its role is still denied. Testing only as sysadmin bypasses the very mapping and permission path being repaired.

For an availability-group environment, prepare the required instance logins on every replica that can become primary. A working current primary does not prove a later failover will work. Include identity mapping in restore and failover rehearsals alongside the data checks.

Contained database users can reduce dependence on instance logins when that authentication model fits the environment, but adopting them is a separate design choice involving connection behavior and security policy. It is not a universal emergency fix.

Keep the verified mapping and validation results with the migration record. A completed restore should mean that the intended application identity can perform its intended work, with its original access boundaries intact.

Technical references: Microsoft Learn: Troubleshoot orphaned users · Microsoft Learn: ALTER USER.

Ask about this article

Have a question about this topic?

Tell us what you are evaluating or where you are stuck. We will respond with a practical recommendation.

Inquiries are not enabled in this preview.

Ask a question about this article