How to rename Azure SQL database

How to rename Azure SQL database

Sometimes, you might have the requirement to rename the Azure SQL database. In this article, we will discuss how to rename the Azure SQL database and along with that, we will also discuss a few other topics as mentioned below.

How to rename Azure SQL database

Before, starting the actual functionality, you should know the prerequisites needed here to perform this operation.

Prerequisites

Assuming you are ready with all the prerequisites needed here, let’s start with the actual functionality.

Well, you can rename the Azure SQL database using Azure Data Studio and Microsoft SQL Server, etc. Now let’s discuss them one by one.

  • Approach-1: Using Azure Data Studio
  • Approach-2: Using SQL Server
  • Approach-3: Rename Azure SQL database PowerShell

Approach-1: Using Azure Data Studio

You can quickly rename the Azure SQL database using Azure Data Studio by following the below steps.

  1. First, connect Azure Data Studio to Azure SQL.
  2. Once you are connected to Azure SQL database, Double click on the Database node or expand the database node to see all the available databases. While expanding the database node, there is a chance you might get the error like “Failure to coonect the server”, if you are getting this error, nothing to worry just right click on the Database node –> click on the Refresh option and then double click the database node or expand the Database node again.

In my case the available databases are master and TsinfoSQL. I want to rename the TsinfoSQL database to TsinfoSQLNew.

azure SQL database how to rename

3. Now click on the New Query option.

SQL azure how to rename database

4. Paste the below SQL query and click on the Run button and you can able to see on the left side the Database name is changed from TsInfoSQL to TsInfoSQLNew.

ALTER DATABASE TsInfoSQL MODIFY NAME = TsInfoSQLNew ;
rename azure sql database

To cross check, you can navigate to the Azure Portal and verify the Azure SQL database name is changed to TsInfoSQLNew.

rename azure sql database in Azure Portal

This is how you can rename the Azure SQL database using the Azure Data Studio.

Approach-2: Using SQL Server

You can also use Microsoft SQL Server to quickly rename your Azure SQL Database using the below steps.

  1. Launch Microsoft SQL Server Management Studio on your machine.
  2. On the Connect to Server window, provide the below details
  • Server type: Choose the server type as Database Engine.
  • Server name: Provide the server name.
  • Authentication: Choose SQL Server Authentication.
  • Login: Provide the username that you have set while creating the Azure SQL database
  • Password: Provide the password that you have set for the username while creating the Azure SQL database.

Finally, click on the Connect button.

Rename SQL Database in Azure

3. Expand the Databases node–> Right click on the Database that you wish to rename –> Select the Rename option as highlighted below.

rename Azure SQL database using SSMS

4. Enter the new name of the Azure SQL database and press the Enter key. Now you can able to see the database name has been changed from TsInfoSQLNew to TsInfoSQLOld.

How to rename Azure SQL database using SSMS

To cross check, Let’s navigate to Azure Portal and go to the Azure SQL database, you can able to see the Azure SQL database name has been changed to TsInfoSQLOld successfully.

rename SQL database in Azure Portal

Approach-3: Rename Azure SQL database PowerShell

Another way to rename your Azure SQL database is by using PowerShell. Follow the below steps.

  1. Run PowerShell ISE with Administrator mode.
  2. Copy paste the below command and make sure to change the server name
Set-AzSqlDatabase -DatabaseName 'Old database name' -NewName 'New database name' -ServerName 'servername' -ResourceGroupName 'Your resource group name'

Example:

Set-AzSqlDatabase -DatabaseName 'TsInfoSQLOld' -NewName 'TsInfoSQLNewDB' -ServerName 'tsinfo' -ResourceGroupName 'DEMORG1'

Once, you will run the above script, you might get the below errors which I also came across. You can fix those errors by following the respective links.

After fixing the errors I ran the above command again and this time I got the expected output as below.

ResourceGroupName                : DEMORG1
ServerName                       : tsinfo
DatabaseName                     : TsInfoSQLNewDB
Location                         : eastus
DatabaseId                       : ceae9749-f92e-4684-a7df-06f9103839d9
Edition                          : GeneralPurpose
CollationName                    : SQL_Latin1_General_CP1_CI_AS
CatalogCollation                 : 
MaxSizeBytes                     : 34359738368
Status                           : Online
CreationDate                     : 11/5/2022 6:26:00 AM
CurrentServiceObjectiveId        : 00000000-0000-0000-0000-000000000000
CurrentServiceObjectiveName      : GP_S_Gen5_1
RequestedServiceObjectiveName    : GP_S_Gen5_1
RequestedServiceObjectiveId      : 
ElasticPoolName                  : 
EarliestRestoreDate              : 11/5/2022 6:36:33 AM
Tags                             : {}
ResourceId                       : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1
                                   /resourceGroups/DEMORG1/providers/Microsoft.Sql/ser
                                   vers/tsinfo/databases/TsInfoSQLNewDB
CreateMode                       : 
ReadScale                        : Disabled
ZoneRedundant                    : False
Capacity                         : 1
Family                           : Gen5
SkuName                          : GP_S_Gen5
LicenseType                      : 
AutoPauseDelayInMinutes          : 60
MinimumCapacity                  : 0.5
ReadReplicaCount                 : 
HighAvailabilityReplicaCount     : 
CurrentBackupStorageRedundancy   : Local
RequestedBackupStorageRedundancy : Local
SecondaryType                    : 
MaintenanceConfigurationId       : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1
                                   /providers/Microsoft.Maintenance/publicMaintenanceC
                                   onfigurations/SQL_Default
EnableLedger                     : False

You can see it here

Rename Azure SQL database PowerShell

To cross-check check the Azure SQL database name is actually changed or not, just verified by navigating to Azure Portal and could able to see that the Azure SQL database name is changed from TsInfoSQLOld to TsInfoSQLNewDB.

How to rename Azure SQL database PowerShell

You may also like following the below articles

Wrapping Up

In this article, we discussed how to rename the Azure SQL database using Azure Data Studio and Microsoft SQL Server. Thanks for reading this article !!!