Migrate from Microsoft SQL Server to Neon Postgres
Learn how to migrate a Microsoft SQL Server database to Neon Postgres using pgloader
This guide describes how to migrate your database from a Microsoft SQL Server (MSSQL) database to Neon Postgres using pgloader.
The pgloader
utility transforms data to a Postgres-compatible format as it reads from your MSSQL database. It uses the Postgres COPY
protocol to stream the data into your Postgres database.
Prerequisites
-
An MSSQL instance containing the data you want to migrate.
For this guide, we use
Azure SQL
, which is a managed cloud-based offering of Microsoft SQL server. We set up an Azure SQL Database and populate it with the Northwind sample dataset. This dataset contains sales data corresponding to a fictional company that imports and exports food products, organized across multiple tables. -
A Neon project to move the data to.
For detailed information on creating a Neon project, see Create a project.
-
Neon's Free Plan supports 500 MiB of data. If your data size is more than 500 MiB, you'll need to upgrade to one of Neon's paid plans. See Neon plans for more information.
-
Review the Pgloader MSSQL to Postgres Guide guide. It will provide you with a good understanding of
pgloader
capabilities and how to configure yourpgloader
configuration file, if necessary. -
See Pgloader configuration for a
pgloader
configuration file update that may be required to connect to MSSQL frompgloader
.
Prepare your MSSQL database
Retrieve Your MSSQL database credentials
Before starting the migration process, collect your MSSQL database credentials. If you are using Azure SQL, you can use the following steps to retrieve them:
- Log into the Azure portal and navigate to your Azure SQL Database resource.
- Navigate to the Connection strings tab under the
Settings
section and identify the connection string for your database. Make note of the following details:- Server
- Database
- User
- Password (Not displayed in the Azure portal)
Keep the database connection details handy for later use.
Allow inbound traffic from Neon
If you are using Azure SQL, you need to allow inbound traffic from your local machine, so pgloader
can connect to your database. To do this, follow these steps:
-
Log into the Azure portal and navigate to your Azure SQL Server resource.
-
Click on the Networking option under the
Settings
section in the sidebar. Navigate to the Firewall Rules section under thePublic access
tab. -
Click on the
Add your Client IPv4 address
option, which will automatically create a new rule with the IP address of your local machine. If you are runningpgloader
elsewhere, replace both theStart IP
andEnd IP
fields with the IP address of that machine. -
CLick
Save
at the bottom to make sure all changes are saved.
Prepare your Neon destination database
This section describes how to prepare your destination Neon PostgreSQL database to receive the migrated data.
Create the Neon database
To maintain parity with the MSSQL deployment, you might want to create a new database in Neon with the same name. Refer to the Create a database guide for more information.
For this example, we will create a new database named Northwind
in the Neon project. Use psql
to connect to your Neon project (alternatively, you can use the Query editor
in the Neon console) and run the following query:
Retrieve your Neon database connection string
Log in to the Neon Console and navigate to the Connection Details section on the Dashboard to find your Postgres database connection string. It should look similar to this:
Now, modify the connection string as follows to pass your endpoint ID (ep-cool-darkness-123456
in this example) to Neon with your password using the endpoint
keyword, as shown here:
note
Passing the endpoint ID
with your password is a required workaround for some Postgres drivers, including the one used by pgloader
. For more information about this workaround and why it's required, refer to our connection workaround documentation.
Keep your Neon connection string handy for later use.
Install pgloader
Here's how you can set up pgloader
for your database migration:
-
Install the
pgloader
utility using your preferred installation method. Debian (apt), RPM package, and Docker methods are supported, as well as Homebrew for macOS (brew install pgloader
). If your macOS has an ARM processor, use the Homebrew installation method.See Installing pgloader for Debian (apt), RPM package, and Docker installation instructions.
-
Create a
pgloader
configuration file (e.g.,mssql_to_neon.load
). Use your MSSQL database credentials to define the connection string for your database source. Use the Neon database connection string as the destination.Example configuration in
mssql_to_neon.load
:Make sure to replace the connection string values with your own MSSQL and Neon credentials.
Run the migration with pgloader
To initiate the migration process, run:
The command output will show the progress of the migration, including any errors encountered and the total time taken. For our sample dataset, the output looks similar to this:
Verify the migration
After the migration is complete, connect to your Neon database and run some queries to verify that the data has been transferred correctly. For example:
This query returns the following result:
Compare the results with the same queries run on your MSSQL database to ensure data integrity.
Clean up
After successfully migrating and verifying your data on Neon:
-
Consider backing up your MSSQL database before decommissioning it.
-
Update your application code to make SQL queries using the Postgres dialect.
-
Update your application's connection strings to point to your new Neon database.
Other migration options
While this guide focuses on using pgloader
, you might need more manual adjustments to ensure:
- There are no unintended changes to the application behavior. For example, all MSSQL data types don't translate one-to-one to Postgres data types.
- The application code is compatible with Neon Postgres.
For complex migrations or when you need more control over the migration process, you might consider developing a custom Extract, Transform, Load (ETL) process using tools like Python with SQLAlchemy.
Pgloader configuration
-
Pgloader
automatically detects table schemas, indexes, and constraints, but depending on the input table schemas, you might need to specify manual overrides in the configuration file. Refer to the Command clauses section of thepgloader
documentation for more information. -
With Azure SQL database,
pgloader
often runs into connection errors. To solve them, you might need to manually specify the FreeTDS driver configuration (whichpgloader
uses to connect to MSSQL). Please refer to the related issues in the PGLoader GitHub repository for more information.Below is the section required to make
pgloader
work, at the time of writing. Replace the values with your own Azure SQL database credentials.
Reference
For more information on pgloader
and database migration, refer to the following resources:
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.