SQL SERVER – Creating a Login and Database User Without Automatic Sysadmin Grants

To Create a Login and User, I separate the instance identity from database access. Database ownership and sysadmin membership have different scopes.

A room key and a separate outer gate key show different access scopes.

-- Administrator templates, not executed during this review.
-- Substitute an existing approved Windows group and target database.
-- USE [master];
-- CREATE LOGIN [YourDomain\ApprovedGroup] FROM WINDOWS;
-- USE [YourDatabase];
-- CREATE USER [ApprovedGroup] FOR LOGIN [YourDomain\ApprovedGroup];
-- ALTER ROLE [db_datareader] ADD MEMBER [ApprovedGroup];

-- Database-wide administrator, only if explicitly required:
-- ALTER ROLE [db_owner] ADD MEMBER [ApprovedGroup];
-- Instance-wide administrator, a separate and much broader grant:
-- USE [master];
-- ALTER SERVER ROLE [sysadmin] ADD MEMBER [YourDomain\ApprovedGroup];

The first path maps a Windows login to a database user with a read-only role. Use narrower object permissions when needed. The administrative grants are separate optional examples. They are not steps everyone should run.

db_owner gives extensive control within one database. sysadmin gives control across the instance and enters databases as dbo. Adding db_owner does not restrict that login to one database. I previously described those scopes too loosely.

My old script used a weak password, created a database, granted both roles, and dropped the database afterward. I no longer recommend that recipe. Use an approved identity and a disposable teaching lab. Treat the old script as historical material.

The login-versus-user video accompanies this article. Verify intended access in a separate connection. Assign an administrative role only when the task requires it.

Related reading

Original video

Database ownership is not instance administration, it is authority within one database.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

, SQL Scripts, SQL Server, SQL Server Security
Previous Post
SQL SERVER – Databases Going to Recovery Pending State Randomly
Next Post
SQL SERVER – Reading Transaction Log to Identified Who Dropped a Table

Related Posts

6 Comments. Leave new

  • Lucas Majewski
    May 16, 2018 8:47 pm

    Uhmm, “You can just run the script as it is and it will work 100%” ? Did you try running it in SSMS connected to Azure SQL database?

    I get these errors:

    Keyword or statement option ‘default_database’ is not supported in this version of SQL Server.

    Keyword or statement option ‘role’ is not supported in this version of SQL Server.

    USE statement is not supported to switch between databases. Use a new connection to connect to a different database.

    Reply
  • does it allow xp cmd shell access if they are not an admin for the entire server or does it still require a proxy account?

    Reply
  • Hi pinal, I need to create a user for 100 databases with read-only in single instance by using script. Please share if it is available

    Reply
  • Tried this on a new laptop with SQL 2017 and I can’t login with NewLogin.

    Login failed for user ‘NewLogin’. (.Net SqlClient Data Provider)

    Reply
  • What if you wanted to use Windows Authentication instead of providing a password, would you just leave off the WITH PASSWORD parameter?

    Reply
  • Hi Sir,

    The Differences between user and login explained crearly. Thank you so much for this video, i learned so much form this.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.