Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL Azure how can I create a read only user

I would like to create an SQL Azure user and grant her read-only access on a handful of DBs, what script can I use to achieve this?

like image 269
Sam Saffron Avatar asked May 05 '10 23:05

Sam Saffron


People also ask

How do I restrict access to Azure SQL Database?

Navigate to https://portal.azure.com and sign in to your subscription. Browse to SQL servers, pick your SQL server, and under Settings, choose Firewall / Virtual Networks. Click OFF under Allow Access to Azure Services and click the Save button. That's it!

How do I create a read access in SQL Server?

Expand Security - Logins, then Right Click on login and then click Properties. Go to User Mapping tab and select the database on which you want to give permission and at bottom select db_datareader as shown below. Click Ok and you're done.


1 Answers

A pure TSQL script is super messy, SQL Azure disables the USE command, so you are stuck opening connections to each DB you need to give the user read access.

This is the gist of the pattern.

In Master DB:

CREATE LOGIN reader WITH password='YourPWD'; -- grant public master access CREATE USER readerUser FROM LOGIN reader; 

In each target DB (requires a separate connection)

CREATE USER readerUser FROM LOGIN reader; EXEC sp_addrolemember 'db_datareader', 'readerUser'; 
like image 166
Sam Saffron Avatar answered Oct 09 '22 21:10

Sam Saffron