You are on page 1of 14

5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance?

- Stack Overflow

How do I grant myself admin access to a local SQL Ask Question

Server instance?
I installed SQL Server 2008 R2 to my local machine. But, I
can't create a new database because of rights (or lack of).
81
"CREATE DATABASE PERMISSION DENIED"

So, I tried to assign the admin privileges to my current


login
30
"User does not have permission to perform this action."

I also tried to create a new login that would have admin


privileges but with no luck. How do I grant myself admin
rights so that I can create a database? I can re-install,
Home
but I prefer not to.
PUBLIC
sql-server-2008 ssms sqlcmd
Stack Overflow

Tags edited May 1 '13 at 21:31


Anthony Mastrean
Users
14.4k 19 82 148
Jobs
asked Mar 27 '12 at 12:13
SkonJeet
2,722 3 17 30
Teams
Q&A for work
Are you trying to connect via SQL Server authentication or
Learn More Windows Authentication? – Darren Mar 27 '12 at 12:18
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 1/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

Windows authentication (see title). I'm connecting successfully.


The errors come from attempting to perform admin tasks. –
SkonJeet Mar 27 '12 at 12:20

6 Answers

Yes - it appears you forgot to add yourself to the sysadmin


role when installing SQL Server. If you are a local
65 administrator on your machine, this blog post can help you
use SQLCMD to get your account into the SQL Server
sysadmin group without having to reinstall. It's a bit of a
security hole in SQL Server, if you ask me, but it'll help
you out in this case.

Edit: Changed dead link to point to archive.org. Original


post here

edited Mar 6 '17 at 20:59


John MacIntyre
10.4k 9 60 99

answered Mar 27 '12 at 12:23


Brian Knight
4,452 21 31

10 It isn't a security hole, local administrators need to have rights


to sql server, in fact a local administrator would anyway be
able to access sql server's data by copying the data files to
another machine (and then copying back if needed), so your
only way around this is to limit administrative permissions
only to the ones who deserve it.. – yoel halb Oct 11 '12 at
20:18

THAT is supposed to be easier than uninstall / reinstall?


Hardly! In my case, we were migrated to another domain; all
existing logins worthless. – dudeNumber4 Jul 30 '15 at 12:49

By using our site, you acknowledge


5 Link isthat you(and
dead havelink-only
read and understand
answers our Cookie Policy, Privacy Policy, and our Terms of Service.
are discouraged).
stackoverflow.com/a/9889484/389424 has the same
https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 2/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow
instructions as the original blog post – janv8000 Jan 17 '17 at
7:52

Open a command prompt window. If you have a default


instance of SQL Server already running, run the following
62 command on the command prompt to stop the SQL Server
service:

net stop mssqlserver

Now go to the directory where SQL server is installed. The


directory can for instance be one of these:

C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVE


C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\M

Figure out your MSSQL directory and CD into it as such:

CD C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSE

Now run the following command to start SQL Server in


single user mode. As SQLCMD is being specified, only one
SQLCMD connection can be made (from another command
prompt window).

sqlservr -m"SQLCMD"

Now, open another command prompt window as the same


user as the one that started SQL Server in single user
mode above, and in it, run:

sqlcmd

And press
By using our site, you acknowledge enter.
that Nowread
you have you and
canunderstand
execute SQL statements
our Cookie Policy, Privacy Policy, and our Terms of Service.
against the SQL Server instance running in single user
https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 3/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

mode:

create login [<<DOMAIN\USERNAME>>] from windows;

-- For older versions of SQL Server:


EXEC sys.sp_addsrvrolemember @loginame = N'<<DOMAIN\USERNAM
N'sysadmin';

-- For newer versions of SQL Server:


ALTER SERVER ROLE [sysadmin] ADD MEMBER [<<DOMAIN\USERNAME>

GO

Source.

UPDATED Do not forget a semicolon after ALTER SERVER


ROLE [sysadmin] ADD MEMBER [<<DOMAIN\USERNAME>>]; and do
not add extra semicolon after GO or the command never
executes.

edited Dec 2 '18 at 3:56


noderman
1,324 12 24

answered Mar 27 '12 at 12:23


Darren
55.2k 16 106 120

1 Thanks Darren, appreciated. – SkonJeet Mar 27 '12 at 12:32

Quick note that sp_addsrvrolemember has been superseded


by ALTER SERVER ROLE . e.g. ALTER SERVER ROLE [sysadmin]
ADD MEMBER [domain\username] – Fermin Mar 30 '17 at 14:19

Don't forget to add the GO; at the end – NMrt Oct 8 '18 at
4:41

Given that many of us are now running SQL Server without


SSMS, I feel this is a much better answer. – Chaim Eliyah Nov
20 '18 at 19:53

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 4/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

I adopted a SQL 2012 database where I was not a


sysadmin but was an administrator on the machine. I used
30 SSMS with "Run as Administrator", added my NT account
as a SQL login and set the server role to sysadmin. No
problem.

answered Aug 15 '14 at 1:30


Vince P.
309 3 5

2 Just used this - worked a treat! – Carole Bennett Nov 5 '14 at


18:52

7 This solution worked for me, with one additional step required:
Had to start sql server in single user mode by adding the "-m"
flag. This can be done using the "SQL Server Configuration
Manager", right click the server instance and select properties,
go to the tab "Startup Parameters" and add -m. – maets May 5
'15 at 13:32

This is awesome. So much simpler than the other answers. –


McKay Jul 15 '15 at 19:29

Definitely fastest and best of all answers – Johnny Jul 22 '15 at


12:37

1 This is actually the correct answer. One note, to get SQL in


single user mode I had to run >net stop
MSSQL$SQLEXPRESS and then >net start
MSSQL$SQLEXPRESS /m – Ev. Dec 1 '16 at 2:31

Here's a script that claims to be able to fix this.

Get admin rights to your local SQL Server Express with


29 this simple script

Download link to the script


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Description
https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 5/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

This command script allows you to easily add yourself


to the sysadmin role of a local SQL Server instance.
You must be a member of the Windows local
Administrators group, or have access to the credentials
of a user who is. The script supports SQL Server 2005
and later.

The script is most useful if you are a developer trying to


use SQL Server 2008 Express that was installed by
someone else. In this situation you usually won't have
admin rights to the SQL Server 2008 Express instance,
since by default only the person installing SQL Server
2008 is granted administrative privileges.

The user who installed SQL Server 2008 Express can


use SQL Server Management Studio to grant the
necessary privileges to you. But what if SQL Server
Management Studio was not installed? Or worse if the
installing user is not available anymore?

This script fixes the problem in just a few clicks!

Note: You will need to provide the BAT file with an


'Instance Name' (Probably going to be 'MSSQLSERVER' -
but it might not be): you can get the value by first running
the following in the "Microsoft SQL Server Management
Console":

SELECT @@servicename

Then copy the result to use when the BAT file prompts for
'SQL instance name'.

@echo off
rem
rem ***************************************************
rem
rem Copyright (c) Microsoft Corporation. All rights
By using our site, you acknowledge
rem thatThis
you have
code read and understand
is licensed our Microsoft
under the Cookie Policy, Privacy Policy, and our Terms of Service.
Public
rem THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY O
https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 6/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow
rem ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING A
rem IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
rem PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
rem
rem ***************************************************
rem
rem CMD script to add a user to the SQL Server sysadmin
rem
rem Input: %1 specifies the instance name to be modifi
rem %2 specifies the principal identity to be a
<user>").
rem If omitted, the script will request elev
user (pre-elevation) to the sysadmin role.
rem If provided explicitly, the script is as
already.
rem
rem Method: 1) restart the SQL service with the '-m' op
connection from a box admin
rem (the box admin is temporarily added to t
start option)
rem 2) connect to the SQL instance and add the
rem 3) restart the SQL service for normal conne
rem
rem Output: Messages indicating success/failure.
rem Note that if elevation is done by this scri
window is created: the output of this
rem window is not directly accessible to the ca
rem
rem
setlocal
set sqlresult=N/A
if .%1 == . (set /P sqlinstance=Enter SQL instance name
else (set sqlinstance=%1)
if .%sqlinstance% == . (set sqlinstance=SQLEXPRESS)
if /I %sqlinstance% == MSSQLSERVER (set sqlservice=MSSQ
sqlservice=MSSQL$%sqlinstance%)
if .%2 == . (set sqllogin="%USERDOMAIN%\%USERNAME%") el
rem remove enclosing quotes
for %%i in (%sqllogin%) do set sqllogin=%%~i
@echo Adding '%sqllogin%' to the 'sysadmin' role on SQL
'%sqlinstance%'.
@echo Verify the '%sqlservice%' service exists ...
set srvstate=0
for /F "usebackq tokens=1,3" %%i in (`sc query %sqlserv
set srvstate=%%j
if .%srvstate% == .0 goto existerror
rem
rem that
By using our site, you acknowledge elevate if <domain/user>
you have was defaulted
read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
rem
https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 7/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow
if NOT .%2 == . goto continue
echo new ActiveXObject("Shell.Application").ShellExecut
\""+WScript.Arguments(0)+"\" & \""+WScript.Arguments(1)+"\
\""+WScript.Arguments(2)+"\"", "", "runas"); >"%TEMP%\addsy
ADE5-C43582022EA8}.js"
call "%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C43582
"%sqllogin%"
del "%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C435820
goto :EOF
:continue
rem
rem determine if the SQL service is running
rem
set srvstarted=0
set srvstate=0
for /F "usebackq tokens=1,3" %%i in (`sc query %sqlserv
set srvstate=%%j
if .%srvstate% == .0 goto queryerror
rem
rem if required, stop the SQL service
rem
if .%srvstate% == .1 goto startm
set srvstarted=1
@echo Stop the '%sqlservice%' service ...
net stop %sqlservice%
if errorlevel 1 goto stoperror
:startm
rem
rem start the SQL service with the '-m' option (single
until its STATE is '4' (STARTED)
rem also use trace flags as follows:
rem 3659 - log all errors to errorlog
rem 4010 - enable shared memory only (lpc:)
rem 4022 - do not start autoprocs
rem
@echo Start the '%sqlservice%' service in maintenance m
sc start %sqlservice% -m -T3659 -T4010 -T4022 >nul
if errorlevel 1 goto startmerror
:checkstate1
set srvstate=0
for /F "usebackq tokens=1,3" %%i in (`sc query %sqlserv
set srvstate=%%j
if .%srvstate% == .0 goto queryerror
if .%srvstate% == .1 goto startmerror
if NOT .%srvstate% == .4 goto checkstate1
rem
rem add the specified user to the sysadmin role
rem that
By using our site, you acknowledge access
youtempdb to avoid
have read a misleading
and understand shutdown
our Cookie error
Policy, Privacy Policy, and our Terms of Service.
rem
https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 8/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow
@echo Add '%sqllogin%' to the 'sysadmin' role ...
for /F "usebackq tokens=1,3" %%i in (`sqlcmd -S np:\\.\
E -Q "create table #foo (bar int); declare @rc int; execute
'$(sqllogin)', 'sysadmin'; print 'RETURN_CODE : '+CAST(@rc
.RETURN_CODE set sqlresult=%%j
rem
rem stop the SQL service
rem
@echo Stop the '%sqlservice%' service ...
net stop %sqlservice%
if errorlevel 1 goto stoperror
if .%srvstarted% == .0 goto exit
rem
rem start the SQL service for normal connections
rem
net start %sqlservice%
if errorlevel 1 goto starterror
goto exit
rem
rem handle unexpected errors
rem
:existerror
sc query %sqlservice%
@echo '%sqlservice%' service is invalid
goto exit
:queryerror
@echo 'sc query %sqlservice%' failed
goto exit
:stoperror
@echo 'net stop %sqlservice%' failed
goto exit
:startmerror
@echo 'sc start %sqlservice% -m' failed
goto exit
:starterror
@echo 'net start %sqlservice%' failed
goto exit
:exit
if .%sqlresult% == .0 (@echo '%sqllogin%' was successfu
role.) else (@echo '%sqllogin%' was NOT added to the 'sysad
%sqlresult%.)
endlocal
pause

edited Jun 9 '15 at 14:38


monojohnny
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
3,297 12 50 74

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 9/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

answered Mar 27 '12 at 12:29


Chris Gessler
18.4k 3 38 72

2 Worked for me. One thing to bear in mind, make sure you
have the .js extension mapped to the Microsoft ® Windows
Based Script Host otherwise this voodoo won't work (I had
mine mapped to notepad.exe). – Phil Cooper Sep 27 '13 at
9:41

1 Links are dead - "The Archive Gallery has been retired". –


stuartd Jul 29 '14 at 12:57

3 @stuartd - added the script – Chris Gessler Jul 30 '14 at 13:38

1 Couldn't find the any key... – colmde Jul 27 '15 at 13:41

2 Someone added this to GitHub. This script is a real-timesaver.


gist.github.com/wadewegner/1677788 – Patrick Mar 28 '17 at
14:45

Microsoft has an article about this issue. It goes through it


all step by step.
4 https://docs.microsoft.com/en-us/sql/database-
engine/configure-windows/connect-to-sql-server-when-
system-administrators-are-locked-out

In short it involves starting up the instance of sqlserver with


-m like all the other answers suggest. However Microsoft
provides slightly more detailed instructions.

From the Start page, start SQL Server Management


Studio. On the View menu, select Registered Servers.
(If your server is not already registered, right-click Local
Server Groups, point to Tasks, and then click Register
Local Servers.)
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 10/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

In the Registered Servers area, right-click your server,


and then click SQL Server Configuration Manager. This
should ask for permission to run as administrator, and
then open the Configuration Manager program.

Close Management Studio.

In SQL Server Configuration Manager, in the left pane,


select SQL Server Services. In the right-pane, find your
instance of SQL Server. (The default instance of SQL
Server includes (MSSQLSERVER) after the computer
name. Named instances appear in upper case with the
same name that they have in Registered Servers.)
Right-click the instance of SQL Server, and then click
Properties.

On the Startup Parameters tab, in the Specify a startup


parameter box, type -m and then click Add. (That's a
dash then lower case letter m.)

Note

For some earlier versions of SQL Server there is no


Startup Parameters tab. In that case, on the Advanced
tab, double-click Startup Parameters. The parameters
open up in a very small window. Be careful not to
change any of the existing parameters. At the very end,
add a new parameter ;-m and then click OK. (That's a
semi-colon then a dash then lower case letter m.)

Click OK, and after the message to restart, right-click


your server name, and then click Restart.

After SQL Server has restarted your server will be in


single-user mode. Make sure that that SQL Server
Agent is not running. If started, it will take your only
connection.

On the Windows 8 start screen, right-click the icon for


By using our site, you acknowledge that youStudio.
Management have read and bottom
At the understand our screen,
of the Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 11/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

select Run as administrator. (This will pass your


administrator credentials to SSMS.)

Note

For earlier versions of Windows, the Run as


administrator option appears as a sub-menu.

In some configurations, SSMS will attempt to make


several connections. Multiple connections will fail
because SQL Server is in single-user mode. You can
select one of the following actions to perform. Do one
of the following.

a) Connect with Object Explorer using Windows


Authentication (which includes your Administrator
credentials). Expand Security, expand Logins, and
double-click your own login. On the Server Roles page,
select sysadmin, and then click OK.

b) Instead of connecting with Object Explorer, connect


with a Query Window using Windows Authentication
(which includes your Administrator credentials). (You
can only connect this way if you did not connect with
Object Explorer.) Execute code such as the following to
add a new Windows Authentication login that is a
member of the sysadmin fixed server role. The
following example adds a domain user named
CONTOSO\PatK.

CREATE LOGIN [CONTOSO\PatK] FROM WINDOWS; ALTER SERVER


sysadmin ADD MEMBER [CONTOSO\PatK];

c) If your SQL Server is running in mixed authentication


mode, connect with a Query Window using Windows
Authentication (which includes your Administrator
credentials). Execute code such as the following to
create a new SQL Server Authentication login that is a
By using our site, you acknowledge
member that
of you
the have read and
sysadmin understand
fixed our Cookie Policy, Privacy Policy, and our Terms of Service.
server role.

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 12/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

CREATE LOGIN TempLogin WITH PASSWORD = '************';


SERVER ROLE sysadmin ADD MEMBER TempLogin;

Warning:

Replace ************ with a strong password.

d) If your SQL Server is running in mixed authentication


mode and you want to reset the password of the sa
account, connect with a Query Window using Windows
Authentication (which includes your Administrator
credentials). Change the password of the sa account
with the following syntax.

ALTER LOGIN sa WITH PASSWORD = '************'; Warning

Replace ************ with a strong password.

The following steps now change SQL Server back to


multi-user mode. Close SSMS.

In SQL Server Configuration Manager, in the left pane,


select SQL Server Services. In the right-pane, right-
click the instance of SQL Server, and then click
Properties.

On the Startup Parameters tab, in the Existing


parameters box, select -m and then click Remove.

Note

For some earlier versions of SQL Server there is no


Startup Parameters tab. In that case, on the Advanced
tab, double-click Startup Parameters. The parameters
open up in a very small window. Remove the ;-m which
you added earlier, and then click OK.

Right-click your server name, and then click Restart.


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 13/14
5/10/2019 ssms - How do I grant myself admin access to a local SQL Server instance? - Stack Overflow

Now you should be able to connect normally with one


of the accounts which is now a member of the
sysadmin fixed server role.

answered Mar 13 '18 at 14:05


Yair Halberstadt
1,133 6 23

Its actually enough to add -m to startup parameters on Sql


Server Configuration Manager, restart service, go to ssms
2 an add checkbox sysadmin on your account, then remove
-m restart again and use as usual.

Database Engine Service Startup Options

-m Starts an instance of SQL Server in single-user


mode.

edited May 2 '16 at 22:11


Mason240
1,727 1 22 37

answered Jan 6 '16 at 14:58


norbertas.gaulia
752 1 7 17

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/9889334/how-do-i-grant-myself-admin-access-to-a-local-sql-server-instance 14/14