Authorize database access to SQL Database, SQL Managed Instance, and Azure Synapse Analytics
In this article, you learn about:
- Options for configuring Azure SQL Database, Azure SQL Managed Instance, and Azure Synapse Analytics (formerly Azure SQL Data Warehouse) to enable users to perform administrative tasks and to access the data stored in these databases.
- The access and authorization configuration after initially creating a new server.
- How to add logins and user accounts in the master database and user accounts and then grant these accounts administrative permissions.
- How to add user accounts in user databases, either associated with logins or as contained user accounts.
- Configure user accounts with permissions in user databases by using database roles and explicit permissions.
Databases in Azure SQL Database, Azure SQL Managed Instance, and Azure Synapse are referred to collectively in the remainder of this article as databases, and the server is referring to the server that manages databases for Azure SQL Database and Azure Synapse.
Authentication and authorization
Authentication is the process of proving the user is who they claim to be. A user connects to a database using a user account. When a user attempts to connect to a database, they provide a user account and authentication information. The user is authenticated using one of the following two authentication methods:
With this authentication method, the user submits a user account name and associated password to establish a connection. This password is stored in the master database for user accounts linked to a login or stored in the database containing the user accounts not linked to a login.
With this authentication method, the user submits a user account name and requests that the service use the credential information stored in Azure Active Directory (Azure AD).
Logins and users: A user account in a database can be associated with a login that is stored in the master database or can be a user name that is stored in an individual database.
- A login is an individual account in the master database, to which a user account in one or more databases can be linked. With a login, the credential information for the user account is stored with the login.
- A user account is an individual account in any database that may be, but does not have to be, linked to a login. With a user account that is not linked to a login, the credential information is stored with the user account.
Authorization to access data and perform various actions are managed using database roles and explicit permissions. Authorization refers to the permissions assigned to a user, and determines what that user is allowed to do. Authorization is controlled by your user account's database role memberships and object-level permissions. As a best practice, you should grant users the least privileges necessary.
Existing logins and user accounts after creating a new database
When you first deploy Azure SQL, you specify an admin login and an associated password for that login. This administrative account is called Server admin. The following configuration of logins and users in the master and user databases occurs during deployment:
- A SQL login with administrative privileges is created using the login name you specified. A login is an individual user account for logging in to SQL Database, SQL Managed Instance, and Azure Synapse.
- This login is granted full administrative permissions on all databases as a server-level principal. The login has all available permissions and can't be limited. In a SQL Managed Instance, this login is added to the sysadmin fixed server role (this role does not exist in Azure SQL Database).
- A user account called
dbois created for this login in each user database. The dbo user has all database permissions in the database and is mapped to the
db_ownerfixed database role. Additional fixed database roles are discussed later in this article.
To identify the administrator accounts for a database, open the Azure portal, and navigate to the Properties tab of your server or managed instance.
The admin login name can't be changed after it has been created. To reset the password for the server admin, go to the Azure portal, click SQL Servers, select the server from the list, and then click Reset Password. To reset the password for the SQL Managed Instance, go to the Azure portal, click the instance, and click Reset password. You can also use PowerShell or the Azure CLI.
Create additional logins and users having administrative permissions
At this point, your server or managed instance is only configured for access using a single SQL login and user account. To create additional logins with full or partial administrative permissions, you have the following options (depending on your deployment mode):
Create an Azure Active Directory administrator account with full administrative permissions
Enable Azure Active Directory authentication and create an Azure AD administrator login. One Azure Active Directory account can be configured as an administrator of the Azure SQL deployment with full administrative permissions. This account can be either an individual or security group account. An Azure AD administrator must be configured if you want to use Azure AD accounts to connect to SQL Database, SQL Managed Instance, or Azure Synapse. For detailed information on enabling Azure AD authentication for all Azure SQL deployment types, see the following articles:
In SQL Managed Instance, create SQL logins with full administrative permissions
In SQL Database, create SQL logins with limited administrative permissions
- Create an additional SQL login in the master database.
- Create a user account in the master database associated with this new login.
- Add the user account to the
loginmanagerrole, or both in the
masterdatabase using the ALTER ROLE statement (for Azure Synapse, use the sp_addrolemember statement).
loginmanagerroles do not pertain to SQL Managed Instance deployments.
Members of these special master database roles for Azure SQL Database have authority to create and manage databases or to create and manage logins. In databases created by a user that is a member of the
dbmanagerrole, the member is mapped to the
db_ownerfixed database role and can log into and manage that database using the
dbouser account. These roles have no explicit permissions outside of the master database.
You can't create an additional SQL login with full administrative permissions in SQL Database.
Create accounts for non-administrator users
You can create accounts for non-administrative users using one of two methods:
Create a login
Create a SQL login in the master database. Then create a user account in each database to which that user needs access and associate the user account with that login. This approach is preferred when the user must access multiple databases and you wish to keep the passwords synchronized. However, this approach has complexities when used with geo-replication as the login must be created on both the primary server and the secondary server(s). For more information, see Configure and manage Azure SQL Database security for geo-restore or failover.
Create a user account
Create a user account in the database to which a user needs access (also called a contained user.
- With SQL Database, you can always create this type of user account.
- With SQL Managed Instance supporting Azure AD server principals, you can create user accounts to authenticate to the SQL Managed Instance without requiring database users to be created as a contained database user.
With this approach, the user authentication information is stored in each database, and replicated to geo-replicated databases automatically. However, if the same account exists in multiple databases and you are using Azure SQL Authentication, you must keep the passwords synchronized manually. Additionally, if a user has an account in different databases with different passwords, remembering those passwords can become a problem.
To create contained users mapped to Azure AD identities, you must be logged in using an Azure AD account that is an administrator in the database in Azure SQL Database. In SQL Managed Instance, a SQL login with
sysadmin permissions can also create an Azure AD login or user.
For examples showing how to create logins and users, see:
- Create login for Azure SQL Database
- Create login for Azure SQL Managed Instance
- Create login for Azure Synapse
- Create user
- Creating Azure AD contained users
For a security tutorial that includes creating users in Azure SQL Database, see Tutorial: Secure Azure SQL Database.
Using fixed and custom database roles
After creating a user account in a database, either based on a login or as a contained user, you can authorize that user to perform various actions and to access data in a particular database. You can use the following methods to authorize access:
Fixed database roles
Add the user account to a fixed database role. There are 9 fixed database roles, each with a defined set of permissions. The most common fixed database roles are: db_owner, db_ddladmin, db_datawriter, db_datareader, db_denydatawriter, and db_denydatareader. db_owner is commonly used to grant full permission to only a few users. The other fixed database roles are useful for getting a simple database in development quickly, but are not recommended for most production databases. For example, the db_datareader fixed database role grants read access to every table in the database, which is more than is strictly necessary.
Custom database role
Create a custom database role using the CREATE ROLE statement. A custom role enables you to create your own user-defined database roles and carefully grant each role the least permissions necessary for the business need. You can then add users to the custom role. When a user is a member of multiple roles, they aggregate the permissions of them all.
Grant permissions directly
Grant the user account permissions directly. There are over 100 permissions that can be individually granted or denied in SQL Database. Many of these permissions are nested. For example, the
UPDATEpermission on a schema includes the
UPDATEpermission on each table within that schema. As in most permission systems, the denial of a permission overrides a grant. Because of the nested nature and the number of permissions, it can take careful study to design an appropriate permission system to properly protect your database. Start with the list of permissions at Permissions (Database Engine) and review the poster size graphic of the permissions.
Efficient access management uses permissions assigned to Active Directory security groups and fixed or custom roles instead of to individual users.
When using Azure Active Directory authentication, put Azure Active Directory users into an Azure Active Directory security group. Create a contained database user for the group. Add one or more database users as a member to custom or builtin database roles with the specific permissions appropriate to that group of users.
When using SQL authentication, create contained database users in the database. Place one or more database users into a custom database role with specific permissions appropriate to that group of users.
You can also use groups for non-contained database users.
You should familiarize yourself with the following features that can be used to limit or elevate permissions:
- Impersonation and module-signing can be used to securely elevate permissions temporarily.
- Row-Level Security can be used limit which rows a user can access.
- Data Masking can be used to limit exposure of sensitive data.
- Stored procedures can be used to limit the actions that can be taken on the database.
For an overview of all Azure SQL Database and SQL Managed Instance security features, see Security overview.