Create a local copy of a database fast
This tutorial assumes you are on a Windows machine
Use Cases
- You want to get a database setup locally without having to manually build out all the schemas and manually seed the data.
- You have a database that you want to copy as a local database.
Description
This tutorial shows you how to use a .bak file to quickly deploy a SQL Server database to a local SQL Server
As an example, I will use the AdventureWorksLT2022 database from Microsoft
Prerequisites
- Have PowerShell installed.
You can probably use differnt CLIs, but I haven't tested that.
- Have docker installed on your machine
- Have SSMS installed
- Have SQL Server running as a Docker container. See this guide for how to do that.
Instructions
-
Obtain the
.bakfile that has all the database info in it. -
Copy the
.bakfile to the docker container by runningdocker cp <your_file> <your_sql_server_container_name>:<target_file_name>in PowerShell.
Replace <your_file> with the file path to the .bak file
Replace <your_sql_server_container_name> with the name of the docker container
Replace <target_file_name> with the name you want to give the file on the docker container filesystem.
-
Go into SSMS and connect to your SQL Server
-
Right click
Databasesand clickRestore Database.... ClickDeviceand then the three dots.... ClickAdd. Scroll to the top and click the root folder/. Click on the.bakfile that you just added to the container's file system. ClickOK->OK. ClickVerify Backup Media. ClickOK.
Now you should be able to see a new database added! Done.
Example
For the example used in this tutorial, we'll assume we're using a SQL Server Docker container with the name my_sql_server_container and the password as password.
- Download the AdventureWorks bak file from here
Assume for the example's sake that this downloads a file called AdventureWorksLT2022.bak to the .\ directory.
-
Run
docker cp .\AdventureWorksLT2022.bak my_sql_server_container:mybakfile.bak -
Connect to SQL Server with SSMS with server name as
.,1433, SQL Server Authentication, login assa, and password aspassword. -
Follow the instructions for step 4.