SQL Server to Postgres – A Step-by-Step Migration Journey

This blog will give you the lowdown on migrating from SQL Server to PostgreSQL. Learn about SQL Server vs Postgres points of comparison, and the numerous advantages of Postgres. We will also show you a manual method to move the data from SQL Server to Postgres and how BryteFlow can do it automatically, without any coding at all.

Quick Links

Think of migrating your data from SQL Server to Postgres? On the face of it, moving to PostgreSQL can be advantageous due to its open-source nature and advanced data types, but MSSQL possesses unique capabilities as well. The selection between the two databases should be made based on the particular application scenario you are considering. Postgres to SQL Server with BryteFlow

What Is Microsoft SQL Server?

Microsoft SQL Server is a relational database management system that can handle large amounts of data, with support for hundreds of simultaneous running cores and petabyte-scale storage. It also has a powerful querying layer for business analysis and integrates well with reporting solutions like Power BI. Recent versions have built-in Spark support, which allows users to analyze massive volumes of data using Spark SQL with minimal additional setup. SQL Server to Databricks (Easy Migration Method)

SQL Server supports the standard SQL language (ANSI SQL) as well as its own proprietary language (T-SQL or Transact SQL). The main interface tool is SQL Server Management Studio (SSMS), which consists of a client and a server. The client sends requests to a SQL Server installed on a system, while the server handles incoming data. About SQL Server CDC
Microsoft SQL is a widely used database management system that has a user-friendly interface. However, it also has some downsides, such as the high cost of ownership for large databases and a strict licensing policy. As a result, some users may consider switching to another DBMS. SQL Server is a popular choice for businesses with strict compliance requirements due to its enterprise-level security and support. BryteFlow for SQL Server

What is PostgreSQL?

PostgreSQL, also known as Postgres, is a free and open-source relational database management system that supports SQL and has many customization options. It stores data using structured objects and is written in the programming language C. The system has a monolithic architecture where all parts work in a logical order. PostgreSQL is an advanced enterprise-class database that supports both relational (SQL) and non-relational (JSON) querying. It is known for its stability, reliability, resilience, performance, and accuracy, a direct result of over thirty years of active community development. PostgreSQL also supports advanced data types and performance optimization similar to those found in commercial databases such as Microsoft SQL Server and Oracle. BryteFlow for PostgreSQL

SQL Server vs Postgres: Which Database Should You Choose?

The question of Postgres vs SQL Server to decide which one is better, arises frequently. Both are relational databases that are widely used. They do have some obvious differences though. PostgreSQL has advantages over SQL Server in various scenarios. Not only is it open-source and free, but it also has several features that are easily accessible and can be implemented automatically. In contrast, Microsoft SQL Server lacks this convenience. Furthermore, PostgreSQL has a more appropriate concurrency management system that can effectively handle cases where multiple processes access and modify shared data simultaneously. The Easy Way to CDC from Multi-Tenant Databases

For small businesses on a budget, PostgreSQL might be a good choice because it is free and offers useful features for managing data. It is easy to install and can be implemented in almost all operating systems. However, businesses that have invested in the Microsoft SQL Server stack may find SQL Server more beneficial than PostgreSQL, since it integrates seamlessly with the Microsoft stack tools that cover reporting, BI, and database management tools. Postgres CDC (6 Easy Methods to Capture Data Changes)

SQL Server vs Postgres: Licensing costs

SQL Server is a commercial product owned by the Microsoft Corporation and needs a paid license to operate. In contrast Postgres is an open-source database, which means it can be used free and permits further modification and distribution. From this perspective Postgres is more affordable than SQL Server. Learn about Oracle to PostgreSQL Migration

SQL Server vs Postgres: Platforms

SQL Server runs mostly on Microsoft Windows, Microsoft Server and Linux. Postgres being open-source, can run on most operating systems like Linux, macOS, Windows, BSD, and Solaris. It can also be used on Docker containers or Kubernetes. Learn about Oracle to SQL Server Migration

SQL Server vs Postgres: Performance and Scalability

SQL Server is perceived to be more adept in managing larger workloads and complex queries than Postgres, this is due to SQL’s features like in-memory OLTP and columnstore indexes that contribute to faster performance. PostgreSQL is highly scalable and provides multiple features for scalability. It can employ several CPU cores for fast, parallel implementation of queries. SQL Server also uses cores, but the standard version only allows upto 24 CPU cores. However the SQL enterprise version allows unlimited amount of CPU cores to be used. About SQL Server CDC

SQL Server vs Postgres: Syntax and Programming Language Support

SQL Server and Postgres both but in addition they also have their own versions of SQL. SQL Server uses Transact-SQL, or T-SQL, which provides all the functionality of SQL and provides many more proprietary programming extensions. In PostgreSQL, you can use SQL along with the Postgres proprietary language PL/pgSQL that enables the creation of functions, trigger procedures, and addition of control structures to SQL. Learn about SQL Server Change Tracking

Postgres offers support for a wider range of programming languages like Python, Perl, PHP, C, C++, Tcl, Net, Java, JavaScript (Node.js), Delphi and more, while SQL Server supports a limited array of languages like Java, JavaScript (Node.js), Python, PHP, C#, C++, and Ruby. PostgreSQL is known for its excellent support of SQL standards and offers a wide range of features, including advanced data types, full-text search, JSON support, and more. Postgres CDC (6 Easy Methods to Capture Data Changes)

SQL Server vs Postgres: Database Type – RDBMS vs. ORDBMS

SQL Server is an RDBMS (Relational Database Management System) and is suitable for handling conventional data processing and administration tasks while Postgres is an ORDBMS (Object-Relational Database Management System). It provides support for the relational model while also supporting object-based concepts like classes, objects, and inheritance. It is suited for management of applications that include complex objects. For instance, it handles structured and non-structured data and can handle new types of data like image files, video, and audio, that an RDBMS cannot deal with. SQL Server to Databricks (Easy Migration Method)

SQL Server vs Postgres: Replication and Availability

SQL Server offers Always On Availability Groups, Database Mirroring and built-in tools for replication and disaster recovery. Postgres too, provides streaming and logical replication, however SQL Server wins out with a better range of high-availability features. About SQL Server Change Tracking

SQL Server vs Postgres: Community and Ecosystem

SQL Server is a product of Microsoft and has a large user base, there is comprehensive support, documentation, and related tools and utilities available for users. In contrast, PostgreSQL has an active open-source community which is always innovating and contributing to Postgres development and maintenance. It also provides a huge array of extensions and plugins that makes life easier and provides high extensibility for various use cases. Migrating Oracle to SQL Server

PostgreSQL Advantages

Let us examine the several advantages of PostgreSQL that make it a popular choice in the market. PostgreSQL’s adaptability enables it to be utilized in a broad spectrum of applications. Furthermore, PostgreSQL possesses distinct features and advantages that render it a viable substitute for other conventional relational database management systems. It is a standards compliant ORDBMS with robust object-oriented and relational database features that make it exceptionally powerful. Here are some of the advantages of PostgreSQL. Postgres to SQL Server with BryteFlow

  • Standards compliance and reliability: PostgreSQL’s high fault-tolerance as a database is attributed to its implementation of write-ahead logging. It adheres to the ACID (Atomicity, Consistency, Isolation, and Durability) principles and offers comprehensive support for views, foreign keys, triggers, joins, and stored procedures in multiple languages. Additionally, PostgreSQL supports various data types of SQL:2008, such as BOOLEAN, NUMERIC, INTEGER, DATE, VARCHAR, TIMESTAMP, INTERVAL, and CHAR.
  • Open-source license:The source code of PostgreSQL is available for free under an open-source license, allowing you to modify, use, and implement it as you wish without any fee. Additionally, there are no licensing costs associated with using PostgreSQL, which eliminates the possibility of over-deployment. In contrast, the cost of using MSSQL Server depends on the number of users and the size of the database. Therefore, for larger databases, you may need to purchase the paid version of MSSQL Server, starting with the Standard edition license. The total cost of using SQL Server is determined by the number of users and the database’s size. Debezium CDC Explained and a Great Alternative CDC Tool
  • Robust extensions: PostgreSQL offers a wide range of powerful features including point-in-time recovery, multi-version concurrency (MVCC), tablespaces, granular access controls, and online/hot backups. In addition, PostgreSQL is capable of handling different language-specific cases, sorting, and formatting. It is highly scalable, allowing it to handle large amounts of data and support a large number of concurrent users. With PostgreSQL, you can create custom functions, define your own data types, and even write code in multiple programming languages without the need to recompile your database. Postgres to Snowflake : 2 Easy Methods of Migration
  • Monolithic architecture: Because of the monolithic architecture of the Postgres database, the components function automatically in conjunction with one another.
  • Great flexibility: Postgres has great flexibility and can be customized extensively to suit different needs. Postgres to SQL Server with BryteFlow
  • Diversity:It has the capability to handle a diverse range of data and document types. Postgres to SQL Server with BryteFlow
  • Works on multiple operating systems: It functions smoothly on various operating systems including Linux, macOS, Windows, BSD, and Solaris.
  • Large open-source community support: PostgreSQL is developed and maintained by an open-source community of developers worldwide. It has a strong and extensive community that provides abundant resources, documentation, and support to its users. This community consistently identifies and fixes bugs, improving the overall security of the database system.
  • Availability in Public Cloud: Flexible open-source licensing and easy availability from public cloud providers like AWS, Microsoft Azure, Google Cloud etc. Aurora Postgres and How to Setup Up Logical Replication
  • General-purpose OLTP database: Postgres is a versatile OLTP database used by both large enterprises and startups as their primary data store to support their internet-scale applications, products, and solutions. The Easy Way to CDC from Multi-Tenant Databases
  • Free open-source Add-ons and Plugins: You can benefit from open-source plugins to enhance your performance. These add-ons are both free and open-source, and they come packed with numerous features.
  • Federated Hub Database: PostgreSQL’s support for JSON and foreign data wrappers enables it to link with other data stores, including NoSQL types, making it an excellent federated hub for polyglot database systems.
  • Geospatial database:When used with the PostGIS extension, PostgreSQL also supports geographic objects and can function as a geospatial data store for GIS and location-based services.
  • Versatility: Enables a variety of data types to be handled, although there are some differences in how they are written and function. This offers users a wider range of choices to manage their data. Postgres to Snowflake : 2 Easy Methods of Migration
  • LAPP Open-Source Stack: PostgreSQL can run dynamic apps and websites as part of a robust alternative to the LAMP stack, known as LAPP. This stack includes Linux, Apache, PostgreSQL, Perl, PHP, and Python.
  • Resilience: It ensures storage reliability by providing numerous fail-safes and redundancies.

Migrating SQL Server to PostgreSQL – Manual Method

There are various methods to transfer your data from SQL Server to Postgres. One option is to manually recreate the database schema in Postgres to match the one in MSSQL. Alternatively, you can also use an automated tool to migrate the data. SQL Server to Databricks (Easy Migration Method)

Manual SQL Server to PostgreSQL Migration

One way to transfer data from SQL Server Management Studio to Postgres is by generating scripts in MSSQL and executing them in Postgres SQL Shell or PgAdmin to create the database schema. However, there may be errors due to differences in data type compatibility between the two databases. Read on to know about SQL Shell, PgAdmin and SQl Server Management Studio. SQL Server to PostgreSQL with BryteFlow

What is SQL Shell?

The SQL Shell is a command-line interface that allows users to connect to and interact with the PostgreSQL database. It provides various functionalities such as connecting to databases, creating, modifying, and deleting databases and tables, managing data by adding, reading, and modifying it, checking the available databases and fields, executing commands from a file, and more, within the PostgreSQL database.

What is PgAdmin?

PGAdmin is an application that comes bundled with a PostgreSQL installation and operates as a web-based graphical user interface (GUI) management tool. It allows users to interact with Postgres and related relational databases, whether they are located on local or remote servers. With PGAdmin, you can efficiently handle all aspects of administration for a Postgres database.

What is SQL Server Management Studio?

SQL Server Management Studio (SSMS) is a unified platform designed to handle various aspects of SQL infrastructure. It serves as a tool for accessing, configuring, administering, and developing all elements related to SQL Servers. With its diverse range of graphical tools and script editors, SSMS offers developers and database administrators of all expertise levels a comprehensive utility to interact with SQL Server.

Alternatively, you can use MSSQL Server Management Studio to export data in CSV, Excel, or text file formats and then import it into Postgres using the COPY TO command or PgAdmin. Postgres offers several methods for importing data from files, including using the COPY command, restoring from a backup, or loading a CSV with a SQL GUI tool. The best approach will depend on your setup and the current state of your data.

The main steps that comprise SQL Server to PostgreSQL Migration are:

  • Exporting table definitions or database from the source MSSQL database.
  • Converting the SQL Server statements to PostgreSQL format.
  • Loading the resulting statements to the PostgreSQL server.
  • Exporting MSSQL data into an intermediate storage.
  • Converting the data into PostgreSQL format and loading it into the PostgreSQL database.

Migrating SQL Server to PostgreSQL – What to keep in mind

Differences in Syntax

To successfully migrate databases, it is important to understand the syntax differences between the source and target databases. If you are migrating manually, you must be cognizant of this. One approach to address this is by creating scripts that convert the data types of SQL Server to the required PostgreSQL data types. Alternatively, you can manually convert the data types after generating the CREATE scripts and before executing them in Postgres. In case you want to utilize functions with the same name as MSSQL in Postgres, you can create a user-defined function with the identical name and write the corresponding function query for Postgres within it. Cloud Migration Challenges

There are many other functions that are different in both, which need to be taken care of. Some of them are given below.

System-Defined Functions Mismatch

MSSQL and PostgreSQL provide different functions for the same thing which needs to be taken care of while migrating.

GETDATE() to get the current date in MSSQL but in PostgreSQL, it is NOW() for that.

To get the UTC date, GETUTCDATE() in MSSQL but in Postgres, the query is SELECT NOW() AT time zone ‘utc’;

To create a unique value of type uniqueidentifier in NEWID() in MSSQL but in Postgres, uuid_generate_v4() comes as an extension.

To check the NULL value,  ISNULL(expression, value) is used in MSSQL but in Postgres, it is COALESCE(expression, value).

Stored Procedures

The stored procedures in Postgres don’t work the same as in MSSQL. To retrieve a query with several columns in a MSSQL stored procedure, we simply write the query within the procedure. However, in Postgres, we need to specify all the input and output parameters. MSSQL allows us to return multiple records through stored procedures, whereas in Postgres, stored procedures cannot return multiple records. To achieve this in Postgres, we must create a function with a return type of TABLE and include the query inside it. Postgres to SQL Server with BryteFlow

String Concatenation

Concatenate data from two or more columns, in MSSQL, the query as:

SELECT firstname + ‘ ‘ + lastname AS employee_name FROM employee; But in Postgres, the query as: SELECT firstname || ‘ ‘ || lastname AS employee_name FROM employee;

Case-sensitivity

Any table name or column name inside double quotes(“”) is taken as case-sensitive in Postgres.

Data Type Mapping

SQL Server and PostgreSQL have varying data types, which means that certain data types in MSSQL cannot directly correspond to those in PostgreSQL. As a result, it is necessary to manually adjust or create scripts to convert the MSSQL data types to their equivalent PostgreSQL data types. Source to Target Mapping Guide (What, Why, How)

Details of Converting Date and Time Data Types

SQL Server PostgreSQL
DATE Date (year, month and day) DATE
DATETIME Date and time with fraction (milliseconds) TIMESTAMP(3)
DATETIME2(p) Date and time with fraction, 0 <= p <= 7, default is 7 TIMESTAMP(p)
DATETIMEOFFSET(p) Date and time with fraction and time zone TIMESTAMP(p) WITH TIME ZONE
SMALLDATETIME Date and time TIMESTAMP(0)

Converting Data Types

SQL Server PostgreSQL
BIGINT 64-bit integer BIGINT
BINARY(n) Fixed-length byte string BYTEA
BIT 1, 0 or NULL BOOLEAN
CHAR(n), CHARACTER(n) Fixed-length character string, 1 ⇐ n ⇐ 8000 CHAR(n), CHARACTER(n)
DECIMAL(p,s), DEC(p,s) Fixed-point number DECIMAL(p,s), DEC(p,s)
DOUBLE PRECISION Double-precision floating-point number DOUBLE PRECISION
FLOAT(p) Floating-point number DOUBLE PRECISION
IMAGE Variable-length binary data, ⇐ 2G BYTEA
INT, INTEGER 32-bit integer INT, INTEGER
MONEY 64-bit currency amount MONEY
NCHAR(n) Fixed-length Unicode UCS-2 string CHAR(n)
NTEXT Variable-length Unicode UCS-2 data, ⇐ 2G TEXT
NUMERIC(p,s) Fixed-point number NUMERIC(p,s)
NVARCHAR(n) Variable-length Unicode UCS-2 string VARCHAR(n)
NVARCHAR(max) Variable-length Unicode UCS-2 data, ⇐ 2G TEXT
REAL Single-precision floating-point number REAL
ROWVERSION Automatically updated binary data BYTEA
SMALLINT 16-bit integer SMALLINT
SMALLMONEY 32-bit currency amount MONEY
TEXT Variable-length character data, ⇐ 2G TEXT
TIME(p) Time (hour, minute, second and fraction) TIME(p)
TIMESTAMP Automatically updated binary data BYTEA
TINYINT 8-bit unsigned integer, 0 to 255 SMALLINT
UNIQUEIDENTIFIER 16-byte GUID (UUID) data CHAR(16)
VARBINARY(n) Variable-length byte string, 1 ⇐ n ⇐ 8000 BYTEA
VARBINARY(max) Variable-length binary data, ⇐ 2G BYTEA
VARCHAR(n) Variable-length character string, 1 ⇐ n ⇐ 8000 VARCHAR(n)
VARCHAR(max) Variable-length character data, ⇐ 2G TEXT
XML XML data XML

There are many more incompatibilities, but we cannot discuss additional differences between SQL Server and PostgreSQL here as it is beyond the scope of this article.

SQL Server to PostgreSQL Migration Steps

Here we show step by step how you can migrate SQL Server to PostgreSQL manually using SQL Server Management Studio, SQL Shell, pgAdmin and the COPY command. The steps:

1.1 Export table definitions or database from the source MSSQL database using MSSQL Server Management Studio.

1.2 Convert the SQL Server statements to PostgreSQL format.

1.3 Load the resulting statements to the PostgreSQL server using SQL Shell.

1.4 Export MS SQL data into intermediate storage – using SQL Server Management Studio.

1.5 Convert it into PostgreSQL format and load into the target database – Using COPY command or pgAdmin.

1.1 Export table definitions or database from the source MSSQL database using SQL Server Management Studio

Export table and schema definitions or entire database from the source SQL database by generating scripts using SQL Server Management Studio.

To generate scripts for a database in SQL Server, follow the steps mentioned below:

Step 1: Open SQL Server Management Studio then connect to the MSSQL Server. Once it is connected, then go to the database that needs to be migrated. Right click on the Database > Tasks > Generate Scripts.

A screenshot of a computer Description automatically generated

Step 2: Then choose the objects for which the scripts have to be generated. You have the option to script the complete database, including all its objects, or selectively script specific objects within the database. Once it is done, click ‘Next’,

A screenshot of a computer Description automatically generated

Step 3: Set the Scripting Option in this step. You have many options to choose from. Either you can open the scripts in a new query window or you can generate the scripts in a single file or one object script in one file. Once the script name and path to create the script are given, then click ‘Next’,

A screenshot of a computer Description automatically generated with medium confidence

Step 4: In the last step, review your objects and settings and then click ‘Finish’. The scripts will be generated in the given location.

1.2 Convert the SQL server statements to PostgreSQL format.

Before loading the script into PostgreSQL, it is necessary to make corrections to the resulting script in the following manner:

  • Remove MS SQL specific statements (i.e. “SET ANSI_NULLS ON”, “SET QUOTED_IDENTIFIER ON”, “SET ANSI_PADDING ON”)
  • Replace square brackets around database object names by double quotes
  • Remove square brackets around types
  • Force to lowercase
  • Replace default MS SQL schema “dbo” by PostgreSQL “public”
  • Remove all non-supported optional keywords (i.e. “WITH NOCHECK”, “CLUSTERED”)
  • Remove all reference to filegroup (i.e. “ON PRIMARY”)
  • Replace types “INT IDENTITY(…)” by “SERIAL”
  • Update all non-supported data types (i.e. “DATETIME” becomes “TIMESTAMP”, “MONEY” becomes NUMERIC(19,4))
  • Replace the MS SQL query terminator “GO” with the PostgreSQL one “;”

1.3 Load the resulting statements to the PostgreSQL server using SQL Shell.

We can load the created statement scripts to PostgreSQL by using SQL Shell.

1.3.1 Using PostgreSQL console client (psql)

The structure of the command line is determined by the format of the script file that is created. If the script file includes a ‘CREATE DATABASE’ statement, you need to use the tool in the following manner, making sure to substitute the placeholder values within curly brackets {…} with the appropriate actual values.

  • psql -h hostname -U username -f {SQL script file name}
    • Otherwise:
  • psql -h hostname -d databasename -U username -f {SQL script file name}

1.4 Export MS SQL data into intermediate storage using SQL Server Management Studio.

We can use the SQL Server Management Studio to copy the data into an intermediate storage before loading into PostgreSQL. Follow the steps mentioned below to export the data:

  • Step 1: – Open MS SQL Server Management Studio. Then connect to the MS SQL Server. Then go to the database that needs to be migrated. Right click on the database > Tasks > Export data.

A screenshot of a computer Description automatically generated with medium confidence

  • Step 2: – In the “Choose a Data Source” window.  Select “SQL Server Native Client” from the Data source drop-down list. Then choose “Windows Authentication”, and your Database from which the data will be exported. Once they are done then click Next,

A screenshot of a computer Description automatically generated with low confidence

  • Step 3:  In the “Choose a Destination” window. At this stage you have three options to export data. CSV, Excel or Flat File. The details of all the three methods are included below,
    • Destination as Flat File: – Select Flat File from the Destination drop-down list. In the File name text box, give the path and file name of the flat file or select the file by browsing. Then choose the file format. Once done, click ‘Next’.

A screenshot of a computer Description automatically generated with medium confidence

Destination as CSV: –CSV files are the simplest files for storing and transferring large amounts of data. Similar to flat file, select Flat File from the Destination drop-down list. In the File name text box, give the path and file name of the CSV file or select the file by browsing. Then give the file extension as ‘.CSV’. Then choose the format. Once done, click ‘Next’.

A screenshot of a computer Description automatically generated with medium confidence

    • Destination as Excel: – Similar to flat file, choose the Microsoft Excel source from the Destination drop-box list.  In the File name text box, give the path and file name of the Excel file or select the file by browsing. Then choose the type of Excel version. Once done, click ‘Next’.

A screenshot of a computer Description automatically generated with medium confidence

  • Step 4: In the “Specify Table Copy or Query step”, choose the “Copy data from one or more tables or views”.
    • To handle binary data in a table, it is necessary to implement a workaround. Choose the option “Compose a query to specify the data for transfer” on the wizard page named “Specify Table Copy or Query”. On the subsequent wizard page titled “Provide a Source Query”, you need to construct a SELECT query in the following manner:

select <non-binary field #1>, <non-binary field #2>, cast(master.sys.fn_varbintohexstr(cast(<binary field name> as varbinary(max))) as varchar(max)) as <binary field name> from <table name>;

Regrettably, this method is not implemented for sizable binary data (1MB or more) since this may lead to the query getting stuck indefinitely.

A picture containing text, electronics, screenshot, display Description automatically generated

  • Step 5: – In the “Select Source Tables and Views” or “Configure Flat File Destination” window:
    • For Excel files, the next window will be “Select Source Tables and Views” where you can choose one or more tables or views to export data from the SQL database to an Excel file.
    • For TXT and CSV files, the next window will be “Configure Flat File Destination”. The following window that appears is called “Configure Flat File Destination” for TXT and CSV files. In this window, you have the option to select one or multiple tables or views for exporting data. After making your selections, you will need to specify the characters that will be used as delimiters for the destination file, both for rows and columns. The Preview button in this window allows you to preview the data that will be exported.

A screenshot of a computer Description automatically generated with medium confidence

  • Step 6: – In the “Save and Run Package” window, you have the option to decide what happens to the data in a SQL Server database. You can choose to export it to a Flat file right away by selecting the “Run immediately” option, or you can save the copied data as an SSIS package by selecting the “Save SSIS package” checkbox. Once you’ve made your choice, you can either click the “Finish” button or proceed to the review step by clicking the ‘Next’ button.

A picture containing text, screenshot, display, software Description automatically generated

  • Step 7: The “Complete the Wizard” window, is the last step in which decisions that were made in the wizard can be verified. Once everything is confirmed, click the ‘Finish’ button to finish the process. Following that, a final step will appear, displaying progress and status details regarding the export of SQL Server data.

1.5 Convert data into PostgreSQL format and load into the target database using COPY command or pgAdmin.

SQL Server and PostgreSQL possess distinct data types, as previously addressed in the section titled ‘Data Type Mismatch’.

To import data into PostgreSQL, the COPY command or pgAdmin can be utilized.

 1.5.1 Method 1: Using the COPY command for Postgres

Import a CSV file in PostgreSQL by connecting to the database using the psql command:

    • The following choice is applicable only when the data is located on the same machine as Postgres. However, if the data is on a remote machine, you must first upload the file to the target machine using the scp command provided below.
      •  $ scp file.csv username@host:/path/to/directory/
  • 1.5.1.1 Option 1: – To import data from a CSV file into an existing table in PostgreSQL, you can utilize the psql command-line tool along with the -c option flag. This flag enables the execution of a specific command string directly.
    • Command:- $ psql -h host_name -U user_name -d database_name -c  “\\copy table_name FROM ‘file_path’ WITH CSV HEADER DELIMITER ‘,'”

Where:

      • host_name is the address of the server the database runs on
      • user_name is the name of the PostgreSQL user you want to execute the command as.
      • database_name is the name of the database you want to connect to.
      • table_name is the name of the table you want to import the data into.
      • file_path is the path to the CSV file you want to load.

1.5.2 Method 2: – Importing a CSV file using pgAdmin

  • 1.5.2.1 Option 1: – To import data from a CSV file using the pgAdmin

To execute an SQL query using the Query Tool in pgAdmin, follow these steps:

    • Open the Query Tool in pgAdmin.
    • Enter your SQL query.
    • Utilize the COPY statement for this purpose.

Step 1: – Open the pgAdmin and login into the Postgres database.

A screenshot of a computer Description automatically generated

Step 2: – Open the query tool by clicking on Tools -> Query Tool

A screenshot of a computer Description automatically generated with medium confidence

Step 3: – Then execute the COPY statement in the “Query Editor”.

      • Ex:- COPY employees FROM ‘C:\Documnets\employees.csv’ DELIMITER ‘,’ CSV HEADER;

Here,

“employees ” is the table name. Specify your table name here.
“C:\Documnets\employees.csv” is the path to your file.
“DELIMITER” is the character used to separate the values in the CSV file.
“HEADER” is the keyword to skip the header (i.e the row containing the column names) when copying data from the CSV.

  • 1.5.2.2 Option 2: – Create the table as well as import data from a CSV file using the pgAdmin

We can create the table as well as import data from a CSV file using the pgAdmin. To do this follow the below steps,

  • Login to the PgAdmin and login to the postgres database.
  • Then simply right-click on your table in the tree on the left and select the Import/Export… menu item.
  • A window will appear with the slider set to Import. Then select the source file and set the format to CSV.
  • Then, set the Header to ‘Yes’ if the file has a header and then select the delimiter.
  • Then, click OK, the data will be imported. And your table is ready in Postgres for analysis.

A screenshot of a computer Description automatically generated with medium confidence

Limitations of manually migrating SQL Server to PostgreSQL

While the previously mentioned approaches enable manual execution of data migration from MSSQL to PostgreSQL, there may be certain obstacles or difficulties that could arise during the process.

  • Data standardization, which includes data transformation and cleaning, cannot be implemented using manual methods.
  • If you want to have real-time data, you will have to create custom code to import data from CSV files as soon as new data becomes available.
  • In order to ensure a reliable transfer of data without any loss, you will need to allocate a portion of your engineering resources to consistently manage and maintain the data flow. Debezium CDC Explained and a Great Alternative CDC Tool

The process of manually migrating a database from Microsoft SQL to PostgreSQL is complex and time-consuming, demanding a significant amount of effort and a fair amount of coding. Converting the data manually is both expensive and slow, posing the risk of producing inaccurate outcomes and potentially leading to data loss or corruption. Thankfully, there are numerous tools available for converting MSSQL to PostgreSQL, enabling the seamless transfer of data between these two database management systems with minimal effort. Bryteflow is one such automated tool that simplifies the SQL Postgres migration process by providing a user-friendly interface, allowing users to complete the conversion with just a few clicks. SQL Server to Databricks (Easy Migration Method)

What to look for in a SQL Data Migration Tool?

We are aware that transferring data involves Extract, Transform, and Load (ETL) procedures, hence it is logical to utilize a data integration tool to streamline this task. Nevertheless, the subsequent query arises: how can one identify the appropriate tool that fulfills their specific needs? Learn about Automating ETL Pipelines
Below are some key factors to consider when selecting the appropriate tool for your organization:

  • Versatile: A good tool should not only be limited to data migration tasks but should also be flexible for use in various ETL processes and integration tasks. These tasks may include cleaning source data or loading data from APIs/third-party services.
  • Included connectors: A business ecosystem consists of multiple cloud applications and database systems, so it is preferable to choose a tool that has pre-built connectors. For example, if you use Microsoft products, it would be beneficial to have a SQL Server connector available. Oracle to SQL Server Migration
  • Cloud compatibility: It should be possible to access and work on your data pipelines from anywhere and at any time. Hence, the tool should support the REST API architecture. Postgres to SQL Server with BryteFlow
  • Easy to learn: The purpose of using data migration tools is to simplify the process and save time compared to developing an in-house solution. Therefore, the tool you select should have a user-friendly interface, ideally with drag-and-drop functionality, to streamline your SQL Server to Postgres migration pipeline. The Easy Way to CDC from Multi-Tenant Databases

BryteFlow moves data from SQL Server to Postgres automatically

BryteFlow is a Cloud-native tool that specializes in the movement of enterprise-scale data from transactional databases (On-Prem and Cloud) like SAPOraclePostgreSQLMySQL  and  SQL Server  to Cloud platforms like  Amazon S3Amazon RedshiftSnowflakeAzure SynapseAzure Data Lake 2PostgreSQLGoogle BigQuerySQL ServerTeradataKafka  and  Databricks. The great thing about BryteFlow is that it is completely automated, and no coding is needed for any process including extraction, SCD type2, masking, or DDL. From SQL sources, BryteFlow replicates data in real-time with a range of Data Capture options including SQL Server Change Data Capture or  SQL Server Change Tracking.  It creates schema and tables automatically on the destination. BryteFlow enables ingestion from all versions of SQL Server to Postgres and supports CDC replication from multi-tenant SQL Server databases as well (unlike most other tools). Oracle to Postgres Migration (The Whys and Hows)

SQL Server to Postgres CDC Options with BryteFlow

BryteFlow as a SQL Server to PostgreSQL Migration Tool – Highlights

Contact us for a Demo or a Free POC of BryteFlow