how to create user in sql server : 2 easy methods

how to create sql user in sql server
how to create sql user
how to create user in sql server using query
how to create user in sql server management studio

A Step-By-Step Guide on Creating Users in SQL Server

how to create user in sql server

In today’s data world, user management of database is the most important role for maintaining data and access control

If you are a SQL developer or database administrator, it is important to understand how to create a user on a SQL server and give permissions to the user.

In this article, we will provide guide to Create user on sql server granting permissions, and Best practices for secure and well-organized database environment

User and Permission in SQL

Before creating a new user in the database, we need to understand why user creation in the SQL server is crucial.
A user in SQL represents that he needs access to the database or application.


Each user can have specific restrictions and some permissions; make sure that only authorized users can connect to the database to manage it.
Access control level is required to control the data security, integrity, with privacy regulations.

Before we start to create user in sql server define the Username what user you are creating and define the permission to that user which permission have to give for created user i.e. datareader or data writer or sysadmin etc.

Connect to SQL Server

The first step for everything change in database is to login to the SSMS. here is same for creating a user in SQL Server connect to the SQL Server instance.

To connect with SQL server , you can use SQL Server Management Studio (SSMS) or SQL client like SQL DBx or that you are comfortable with.

Here’s how to connect:

Open SQL Server Management Studio or your preferred SQL client like SQL Dbx .

provide the necessary server information, like server Type(Database Engine), server name(your server instance name or ip) and authentication type (SQL server Authentication or Windows Authentication) and then ‘user_name’ and ‘password’ , Click “Connect” to establish the connection.

Make sure that you needs to connect with your admin user to manage users in sql server which has full permissions like ‘sysadmin’ or ‘sa’ user.

Creating a Login

Step1.Expand Securities and right-click on login and select New Login

create login in ssms step1

In SQL Server, the process of creating a new user starts with creating a login.

Step2. in General Option choose login type windows or sql server authentication and then enter login name and password with confirm password.

create login step 2 enter username and password

Step 3. In server roles options you can set server role to new created user what type of user is that, if you are creating general user for select and insert update queries keep server role public.

select  server roles for user

Step4. In User Mapping select database and assign permissions to user, here is we have provided dd_datareader and db_datawriter permission to our Testuser.

grant permissions to user

Step5. IN this step you can Grant or Deny the above given permissions and server roles to the Testuser and click on Ok.

grant and deny setting

A login is a safety important on the server stage, and it’s first step access point for getting access to the SQL Server instance.
SQL user is used to login SQL instance and manage database
To create a new user in sql server, you can use the CREATE LOGIN statement.

Here’s an example of how to Create Login in sql server:

how to create user in sql server using query

CREATE LOGIN Username WITH PASSWORD = 'Password';

In this declaration, replace “Username” with the preferred username for the brand new user and “Password” with a robust and secure password. It’s crucial to pick robust passwords to protect the user’s account from unauthorized access.

Once you’ve executed this command, SQL Server will create the login, making it feasible for the user to connect with the server.

Assigning Permissions

Creating a login is just the first step. To manage what a user can do inside the database, you want to assign suitable permissions. Permissions in SQL Server are frequently controlled thru roles.

There are integrated roles like db_datareader and db_datawriter, and you could also create custom roles to fit your particular desires.

Let’s consider you want to grant your user both read and write access to a specific database.

You can do this by using below sql query:

USE DatabaseName;

ALTER ROLE db_datareader ADD MEMBER Username;
ALTER ROLE db_datawriter ADD MEMBER YourUsername;

In this example, replace “YourDatabaseName” with the name of the database you want the user to access. The ALTER ROLE statements add the user to the specified roles, granting them the necessary permissions.

or you can give the permission using SSMS in Permissions tab allow data_reader and datawriter for specific sellected database.

Once you have given read and write permission to that user for defined database user can only write select and insert sql queries on that database

https://learn.microsoft.com/

Example of Create user with permission

USE [master]
GO
CREATE LOGIN [Testuser] WITH PASSWORD=N'XtNr%43@5' MUST_CHANGE, DEFAULT_DATABASE=[master], CHECK_EXPIRATION=ON, CHECK_POLICY=ON
GO
use [EMPDB];
GO
use [master];
GO
USE [EMPDB]
GO
CREATE USER [Testuser] FOR LOGIN [Testuser]
GO
USE [EMPDB]
GO
ALTER ROLE [db_datareader] ADD MEMBER [Testuser]
GO
USE [EMPDB]
GO
ALTER ROLE [db_datawriter] ADD MEMBER [Testuser]
GO
create user using sql query
Working with Databases

Users are regularly related to specific databases, and the permissions you supply to a sql user are usually tied to a selected database.

Therefore, it is essential to understand the way to transfer between databases and provide database-level permissions.

To switch to a specific database, you can try the USE statement:

USE  DatabaseName;

use EMPDB;

This command allows you change your database to use or change current database to another one which you want to use.
In some cases you have multiple databases in that case you can use the “USE DatabaseName” command or you can change the database manually in SSMS.

To furnish database-stage permissions, you can use statements like GRANT or REVOKE to control what the user can do inside that particular database.

Remember that the principal of least privilege is crucial.
Make sure to grant permission to user absolutely needs to perform their tasks.
This will reduce the risk of security for unnecessary or fraud modification to database.

Best Practices

As you create users and manage their access, it’s important to follow below factors:

Regular Password Updates: Encourage users to change their passwords periodically to increase security protection.

Least Privilege Principle: Grant only user task level permissions to user as per their level or wok. Only grant the permissions required for a user’s task, avoid to grant unnecessary permisson as this can lead to data vulnerabilities.

Audit and Monitor: Keep watching and monitor user accounts and database activities, regularly review the sql script and DBA level changes and audit user privileges to maintain a secure environment.

Click Here how to Backup Database

Data Backup and Recovery: Ensure that data backups are in place and regularly make sure to create daily backups . This helps in case of accidental data loss or corruption at this time you can restore the backup. using “SQL Restore


Click Here How to Restore Database

Benefits of Create User in sql

Creating users in SQL Server is a basic task for database administrators and developers .

It provides the necessary access control to protect sensitive data and maintain database integrity.

User Creation is provides required access to protect database against the data privacy and fraud this will help to protect sensitive data and maintain database security.


if user is created then you can maintain database change ,modification logs for tracking.

Don’t use ‘sa’ user by default create same user like ‘sa’ with sysadmin role this will helpful for unauthorized access to your database because by default user for sql server is ‘sa’ and this known to all.

Leave a Comment