8

how do you create a new database user with password in sql server 2005?

i will need this user/password to use in the connection string eg:

uid=*user*;pwd=*password*;

6 Answers 6

16
CREATE LOGIN [user] WITH PASSWORD='password', 
       DEFAULT_DATABASE=[your_db], CHECK_POLICY=OFF
GO

CREATE USER [user] FOR LOGIN [user]
EXEC sp_addrolemember N'db_datareader', N'your_db'
EXEC sp_addrolemember N'db_datawriter', N'your_db'
GO

Where CHECK_POLICY=OFF switches off password complexity check, etc

Sign up to request clarification or add additional context in comments.

2 Comments

I agree. This method is superior to the sp_addlogin method because you can turn off the windows account policy.
Note that sp_addrolemember is deprecated and should not be used anymore. The MS doc recommends ALTER ROLE instead.
6

As of SQL Server 2005, you should basically create users in two steps:

  • create a "login" to your SQL Server as a whole
  • create users for this login in each database needed

You'd go about doing this like so:

CREATE LOGIN MyNewUser WITH PASSWORD = 'top$secret';

And the "USE" your database and create a user for that login:

USE AdventureWorks;
CREATE USER MyNewUser FOR LOGIN MyNewUser

Comments

2

As indicated, use the CREATE LOGIN to create the ability to connect to SQL Server as that account. Then use CREATE USER within the database to give that login the ability to access the database in question.

However, a few security points based on some of these comments:

  • If at all possible, you want to use Windows authentication, not a SQL Server based login (which is what you are doing when you use user/pwd in this manner). If you are running from a computer on the same domain as SQL Server, you can use a service account that is a Windows user account. This ensures the domain is the single source for security.
  • You didn't say what rights the user needed. Avoid using db_datareader and db_datawriter roles whenever possible. They give IMPLICIT access to tables and views and if someone is performing a quick permissions check on the database, they may not think to check the membership in these roles. That means your reporting on security is using. Best practices say to create your own database role, assign permissions to it, and make the user a member of that role.
  • Whenever possible, use a strong password. One example had the password policies turned off. SQL Server will use the password policy from the local server (which is usually set at the domain level). You want to maintain that strong password policy, if possible.

Comments

1

You'll have to create it first as a user, and then set up the correct permissions for the user.

  1. you'll have to ensure that your DB is configured with both User auth and SQL auth If using the Management Studio: right-click on the Server, select "Security" ensure that server authentication is "SQL Server and Windows Authentication mode"

  2. in Security-logins, right click and select "New Login", select SQL Authentication, use the username and password you like.

    USE [master]
    GO
    CREATE LOGIN [ test] WITH PASSWORD=N'test', DEFAULT_DATABASE=[MY_DATABASE], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
    GO 
    
  3. on the DB you want, in security, users, select new User. Select a username, and attach the login name you've just created, and select the roles you want to apply to this user (i.e. db_datareader, db_datawriter):

    USE [MY_DATABASE]
    GO
    CREATE USER [myDefaultUser] FOR LOGIN [ test]
    GO
    USE [MY_DATABASE]
    GO
    EXEC sp_addrolemember N'db_datareader', N'myDefaultUser'
    GO
    USE [MY_DATABASE]
    GO
    EXEC sp_addrolemember N'db_datawriter', N'myDefaultUser'
    GO
    

That is it. Now you can create your connection string using this password.

1 Comment

Note that sp_addrolemember is deprecated and should not be used anymore. The MS doc recommends ALTER ROLE instead.
0
CREATE LOGIN MyNewUser WITH PASSWORD = 'top$secret'

USE AdventureWorks 
CREATE USER MyNewUser FOR LOGIN MyNewUser 
GO

1 Comment

I see you're relatively new here. It's always good practice to examine the prior answers before posting your own. When doing so, be sure your answer has some unique information, be it in the approach, clarity, or completeness of your answer versus others.
-1
USE [MASTER]


EXEC master.dbo.sp_addlogin @loginame = N'USERNAME', @passwd = 'THEPASS' @defdb = N'master', @deflanguage = N'us_english'


USE [YOUR_DB]
    EXEC dbo.sp_grantdbaccess @loginame = N'USERNAME', @name_in_db = N'USERNAME'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.