I installed SQL Server 2008 R2 to my local machine. But, I can't create a new database because of rights (or lack of).
"CREATE DATABASE PERMISSION DENIED"
So, I tried to assign the admin privileges to my current login
"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, but I prefer not to.
Yes - it appears you forgot to add yourself to the sysadmin role when installing SQL Server. If you are a local 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
Microsoft has an article about this issue. It goes through it all step by step.
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.Its actually enough to add -m to startup parameters on Sql Server Configuration Manager, restart service, go to ssms an add checkbox sysadmin on your account, then remove -m restart again and use as usual.
Database Engine Service Startup Options
Open a command prompt window. If you have a default instance of SQL Server already running, run the following command on the command prompt to stop the SQL Server service:
Now go to the directory where SQL server is installed. The directory can for instance be one of these:
Figure out your MSSQL directory and
CD
into it as such:Now run the following command to start SQL Server in single user mode. As
SQLCMD
is being specified, only oneSQLCMD
connection can be made (from another command prompt window).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:
And press enter. Now you can execute SQL statements against the SQL Server instance running in single user mode:
Source.
UPDATED Do not forget a semicolon after
ALTER SERVER ROLE [sysadmin] ADD MEMBER [<<DOMAIN\USERNAME>>];
and do not add extra semicolon afterGO
or the command never executes.Here's a script that claims to be able to fix this.
Get admin rights to your local SQL Server Express with this simple script
Download link to the script
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":
Then copy the result to use when the BAT file prompts for 'SQL instance name'.
I adopted a SQL 2012 database where I was not a sysadmin but was an administrator on the machine. I used SSMS with "Run as Administrator", added my NT account as a SQL login and set the server role to sysadmin. No problem.