---
title: "Join a Secondary Database to an Availability Group (SQL Server) | Microsoft Docs"
ms.custom: ""
ms.date: "05/17/2016"
ms.prod: "sql-server-2016"
ms.reviewer: ""
ms.suite: ""
ms.technology:
- "dbe-high-availability"
ms.tgt_pltfrm: ""
ms.topic: "article"
f1_keywords:
- "sql13.swb.availabilitygroup.joindbs.f1"
helpviewer_keywords:
- "secondary databases [SQL Server], in availability group"
- "secondary databases [SQL Server]"
- "Availability Groups [SQL Server], joining"
- "Availability Groups [SQL Server], configuring"
- "Availability Groups [SQL Server], databases"
ms.assetid: fd7efe79-c1f9-497d-bfe7-b2a2b2321cf5
caps.latest.revision: 39
author: "MikeRayMSFT"
ms.author: "mikeray"
manager: "jhubbard"
---
# Join a Secondary Database to an Availability Group (SQL Server)
This topic explains how to join a secondary database to an Always On availability group by using [!INCLUDE[ssManStudioFull](../../../includes/ssmanstudiofull-md.md)], [!INCLUDE[tsql](../../../includes/tsql-md.md)], or PowerShell in [!INCLUDE[ssCurrent](../../../includes/sscurrent-md.md)]. After you prepare a secondary database for a secondary replica, you need to join the database to the availability group as soon as possible. This will start data movement from the corresponding primary database to the secondary database.
- **Before you begin:**
[Prerequisites](#Prerequisites)
[Security](#Security)
- **To prepare a secondary database, using:**
[SQL Server Management Studio](#SSMSProcedure)
[Transact-SQL](#TsqlProcedure)
[PowerShell](#PowerShellProcedure)
> [!NOTE]
> For information about what happens after a secondary database joins the group, see [Overview of Always On Availability Groups (SQL Server)](../../../database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server.md).
## Before You Begin
### Prerequisites
- You must be connected to the server instance that hosts the secondary replica.
- The secondary replica must already be joined to the availability group. For more information, see [Join a Secondary Replica to an Availability Group (SQL Server)](../../../database-engine/availability-groups/windows/join-a-secondary-replica-to-an-availability-group-sql-server.md).
- The secondary database must have been prepared recently. For more information, see [Manually Prepare a Secondary Database for an Availability Group (SQL Server)](../../../database-engine/availability-groups/windows/manually-prepare-a-secondary-database-for-an-availability-group-sql-server.md).
### Security
#### Permissions
Requires ALTER AVAILABILITY GROUP permission on the availability group, CONTROL AVAILABILITY GROUP permission, ALTER ANY AVAILABILITY GROUP permission, or CONTROL SERVER permission.
## Using SQL Server Management Studio
**To join a secondary database to an availability group**
1. In Object Explorer, connect to the server instance that hosts the secondary replica, and expand the server tree.
2. Expand the **Always On High Availability** node and the **Availability Groups** node.
3. Expand the availability group that you want to change, and expand the **Availability Databases** node.
4. Right-click the database, and click **Join to Availability Group**.
5. This opens the **Join Databases to Availability Group** dialog box. Verify the availability group name, which is displayed on the title bar, and database name or names displayed in the grid, and click **OK**, or click **Cancel**.
## Using Transact-SQL
**To join a secondary database to an availability group**
1. Connect to the server instance that hosts the secondary replica.
2. Use the [SET HADR clause of the ALTER DATABASE](../../../t-sql/statements/alter-database-transact-sql-set-hadr.md) statement, as follows:
ALTER DATABASE *database_name* SET HADR AVAILABILITY GROUP = *group_name*
where *database_name* is the name of a database to be joined and *group_name* is the name of the availability group.
The following example joins the secondary database, `Db1`, to the local secondary replica of the `MyAG` availability group.
```
ALTER DATABASE Db1 SET HADR AVAILABILITY GROUP = MyAG;
```
> [!NOTE]
> To see this [!INCLUDE[tsql](../../../includes/tsql-md.md)] statement used in context, see [Create an Availability Group (Transact-SQL)](../../../database-engine/availability-groups/windows/create-an-availability-group-transact-sql.md).
## Using PowerShell
**To join a secondary database to an availability group**
1. Change directory (**cd**) to the server instance that hosts the secondary replica.
2. Use the **Add-SqlAvailabilityDatabase** cmdlet to join one or more secondary databases to the availability group.
For example, the following command joins a secondary database, `Db1`, to the availability group `MyAG` on one of the server instances that hosts a secondary replica.
```
Add-SqlAvailabilityDatabase `
-Path SQLSERVER:\SQL\SecondaryServer\InstanceName\AvailabilityGroups\MyAG `
-Database "Db1"
```
> [!NOTE]
> To view the syntax of a cmdlet, use the **Get-Help** cmdlet in the [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] PowerShell environment. For more information, see [Get Help SQL Server PowerShell](../../../relational-databases/scripting/get-help-sql-server-powershell.md).
**To set up and use the SQL Server PowerShell provider**
- [SQL Server PowerShell Provider](../../../relational-databases/scripting/sql-server-powershell-provider.md)
## Related Tasks
- [Join a Secondary Replica to an Availability Group (SQL Server)](../../../database-engine/availability-groups/windows/join-a-secondary-replica-to-an-availability-group-sql-server.md)
- [Manually Prepare a Secondary Database for an Availability Group (SQL Server)](../../../database-engine/availability-groups/windows/manually-prepare-a-secondary-database-for-an-availability-group-sql-server.md)
## See Also
[ALTER AVAILABILITY GROUP (Transact-SQL)](../../../t-sql/statements/alter-availability-group-transact-sql.md)
[Overview of Always On Availability Groups (SQL Server)](../../../database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server.md)
[Troubleshoot Always On Availability Groups Configuration (SQL Server)](../../../database-engine/availability-groups/windows/troubleshoot-always-on-availability-groups-configuration-sql-server.md)