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 functionality, you should know the prerequisites to perform this operation.

Prerequisites

Let’s start with the actual functionality if you are ready with all the prerequisites.

You can rename the Azure SQL database using Azure Data Studio, 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 the 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 an error like “Failure to connect the server.” If you are getting this error, there is nothing to worry about; 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.

rename azure sql database

3. Now click on the New Query option.

azure sql rename database

4. Paste the below SQL query and click on the Run button. 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, navigate to the Azure Portal and verify the Azure SQL database name has been changed to TsInfoSQLNew.

azure data studio rename database

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 rename your Azure SQL Database using the steps below quickly.

  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 you set for the username while creating the Azure SQL database.

Finally, click on the Connect button.

azure rename sql database

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

rename azure sql database from portal

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

azure rename database

To cross-check, navigate to Azure Portal and the Azure SQL database. The Azure SQL database name has been changed to TsInfoSQLOld successfully.

azure sql database rename

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 and paste the below command, and 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 run the above script, you might get the below errors, which I also encountered. You can fix those errors by following the respective links.

After fixing the errors, I reran the above command, 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 whether the Azure SQL database name is changed, verify by navigating to Azure Portal and seeing that the Azure SQL database name is changed from TsInfoSQLOld to TsInfoSQLNewDB.

azure sql server rename database

You may also like following the articles below

Wrapping Up

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