in Microsoft SQL Server

Re-map user to login in SQL server after restoring or attaching database

Re-map database user to a login in SQL server after restoring or attaching database

When restoring or attaching a database to a new or different Microsoft SQL server the logins and mappings to the database users are lost. If you create the logins again you’re not able to map the users because they already exist in the database:

User, group, or role '{login}' already exists in the current database.

Here’s the solution:

1. Create the logins with correct passwords

2. Open a new query window and execute this query

USE {database};
ALTER USER {user} WITH login = {login}

{database}: The database containing the orphan user
{user}: The orphan user name
{login}: The login name. You can use the same login as used on the old server or map the user to a different login name