DEV Community

Hadi Atef
Hadi Atef

Posted on

Compiling PostgreSQL extensions on Windows with MSVC

Introduction

PostgreSQL is an open-source relational database management system that is widely used in enterprise applications. PostgreSQL supports extensibility, which allows developers to extend its functionality by creating custom extensions. Developing PostgreSQL extensions on Windows with MSVC (Microsoft Visual C++) can be a challenging task for some developers.

Step 1: Download the PostgreSQL source code
First, we need to download the PostgreSQL source code. We can download the source code from the PostgreSQL website or use Git to clone the source code repository.

Step 2: Set up the PostgreSQL build environment
Once we have the PostgreSQL source code, we need to set up the build environment. To do this, we need to run the following command in the PostgreSQL source code directory:

build\msvc\setenv.cmd

This will open a new command prompt window with the environment variables set up for building PostgreSQL with MSVC.

Step 3: Build the PostgreSQL source code
To build the PostgreSQL source code, we need to run the following command in the PostgreSQL source code directory:
nmake /f win32.mak

This will start the build process, which may take several minutes to complete depending on the speed of your machine.

Step 4: Create a new Visual Studio project for the extension
Once the PostgreSQL source code is built, we can create a new Visual Studio project for our extension. To do this, we need to follow these steps:

  1. Open Visual Studio and select "File" > "New" > "Project".
  2. Select "Visual C++" > "Dynamic-link Library (DLL)" and enter a name for the project.
  3. Select "DLL" for the "Application type" and "Empty Project" for the "Additional options".
  4. Click "Create" to create the new project.

Step 5: Add the PostgreSQL include and library directories to the project
To use the PostgreSQL libraries in our extension, we need to add the PostgreSQL include and library directories to the project. To do this, we need to follow these steps:

  1. Right-click on the project in the "Solution Explorer" and select "Properties".
  2. Select "VC++ Directories" > "Include Directories" and add the path to the PostgreSQL include directory.
  3. Select "Linker" > "General" > "Additional Library Directories" and add the path to the PostgreSQL lib directory.

Step 6: Add the extension source code to the project
Next, we need to add the extension source code to the project. To do this, we can either copy the source code to the project directory or add the source files to the project using the "Add Existing Item" option.

Step 7: Build the extension
Finally, we can build the extension by clicking on "Build" > "Build Solution" in Visual Studio. If everything is set up correctly, the extension should build without any errors.

Conclusion

Compiling PostgreSQL extensions on Windows with MSVC can be a daunting task, but with the right tools and steps, it can be a straightforward process.

Top comments (0)