[Solved] Unable to install pgvector extension for PostgreSQL [Windows] – Postgresql

by
Ali Hasan
nmake pgvector postgresql vector-database

Quick Fix: To install pgvector extension for PostgreSQL on Windows, open command prompt as admin and run the following commands:

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

set "PGROOT=C:\Program Files\PostgreSQL\16"

git clone --branch v0.5.0 https://github.com/pgvector/pgvector.git

cd pgvector

nmake /F Makefile.win

nmake /F Makefile.win install

The Problem:

Facing difficulties installing the pgvector extension for PostgreSQL on Windows. Using the nmake /F Makefile.win command results in ‘cannot open include file ‘crtdefs.h’: No such file or directory’ error. Additionally, installing using conda leads to an error while creating the vector extension. crtdefs.h is located at ‘C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.36.32532\include’. INCLUDE and LIB environment variables have been added with paths from Microsoft Visual Studio and Windows Kits. Running nmake /F Makefile.win now results in ‘fatal error LNK1104: cannot open file ”vector.dll” ‘ error. The vector.dll file is absent in the pgvector folder.

The Solutions:

Solution 1: Run the Command Prompt as Administrator

To resolve the issue, you need to execute the command prompt as an administrator. This will elevate your privileges and allow you to successfully install the pgvector extension. Here’s the updated solution:

  1. Open Command Prompt as Administrator:

    • Search for "Command Prompt" in the Windows search bar.
    • Right-click on "Command Prompt" and select "Run as administrator."
  2. Set Environment Variables:

    • In the elevated Command Prompt, set the PGROOT environment variable to the path of your PostgreSQL installation directory. For example:
    set "PGROOT=C:\Program Files\PostgreSQL\15"
    
  3. Clone pgvector Repository:

    • Clone the pgvector repository from GitHub to your local system:
    git clone --branch v0.5.0 https://github.com/pgvector/pgvector.git
    
  4. Build and Install pgvector:

    • Change directory to the cloned pgvector folder:
    cd pgvector
    
    • Build the pgvector extension:
    nmake /F Makefile.win
    
    • Install the pgvector extension:
    nmake /F Makefile.win install
    
  5. Restart PostgreSQL:

    • Restart the PostgreSQL service for the changes to take effect.

After following these steps, the pgvector extension should be successfully installed on your PostgreSQL server.

Solution 2: Install Correct Win SDK

If you encounter the error "fatal error LNK1104: cannot open file ‘vector.dll’", it indicates that the correct Win SDK is not installed. To resolve this issue:

  • Open the Visual Studio Installer.
  • Select "Desktop development" and ensure it is installed.
  • Add the Windows 10 SDK version 16 or higher.

Solution 3: Visual Studio Setup

  1. Install the C/C++ extension for Visual Studio Code.

  2. Copy the provided command into your command prompt (run as administrator) to install the necessary Visual Studio Build Tools:

    winget install Microsoft.VisualStudio.2022.BuildTools --force --override "--wait --passive --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows11SDK.22000"
    
  3. Ensure the Developer Command Prompt for VS is installed and functional. Verify using cl in the command prompt.

  4. Run the following commands in your git bash:

    git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git
    cd pgvector
    call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    set "PGROOT=C:\Program Files\PostgreSQL\15"
    nmake /F Makefile.win
    nmake /F Makefile.win install
    

    (Adjust the PGROOT path as appropriate.)

  5. Finally, install pgvector in conda using the provided command:

    conda install -c conda-forge pgvector
    
  6. Create the extension in your database:

    CREATE EXTENSION IF NOT EXISTS vector
    
  7. Check the Functions directory to confirm the extension is present.

Solution 4: Reset Local Build Environment

The issue arises due to missing build tools and incorrect environment setup for C++ development. Here’s the detailed solution:

  1. Download and install the "Build Tools for Visual Studio 2022" from Microsoft’s website to set up a local C++ build environment.

  2. Start a Command Prompt (CMD) with administrator privileges to set the necessary environment variables and run the build commands.

  3. Set the PGROOT environment variable to the directory where PostgreSQL is installed. For example:

    set "PGROOT=C:\Program Files\PostgreSQL\15"
    
  4. Navigate to the directory where the pgvector source code is located.

  5. Run the following commands to compile the pgvector extension’s DLL and install it into PostgreSQL:

    nmake /F Makefile.win
    nmake /F Makefile.win install
    
  6. Ensure that the vcvars64.bat file is present in the installed Build Tools directory. This file is crucial for setting up the C++ build environment.

By following these steps, you can correctly install the pgvector extension on Windows, resolving the missing include files and DLL compilation issues.

Q&A

How to install pgvector for PostgreSQL on Windows?

Open command prompt as admin and call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

What is the reason for the error "Cannot open file ‘vector.dll’"?

Make sure to install the correct Win SDK, if you see this error.

How to set up a C++ environment on Windows?

Download "Build Tools for Visual Studio 2022" and run "Call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat""

Video Explanation:

The following video, titled "Postgres pgvector Extension - Vector Database with PostgreSQL ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Edit: - Problem 1: My postgres container is within WSL2, which I cannot connect with PgAdmin from Windows - Solution : connect pgAdmin page ...