DEV Community

Cover image for How to add pg_embedding extension to PostgreSQL
bkmalan
bkmalan

Posted on

How to add pg_embedding extension to PostgreSQL

The new pg_embedding extension was recently released, bringing exciting advancements by boosting graph-based approximate nearest neighbor search speed by 20x with 99% accuracy for your Postgres databases.

Open-source extensions like pgvector and pg_embedding facilitate vector similarity search within Postgres, making it a robust choice for storing vectors and executing similarity searches. However, their indexing methods vary, so it's essential to explore their differences and choose the most suitable one for your needs more on that here. The installation process for both extensions remains the same.

You can also install this on Postgres running on Docker, skip to the section below.

Let's get started.

Step 1: Begin by cloning pg_embedding from its repository and move it to /tmp directory:

git clone https://github.com/neondatabase/pg_embedding.git
mv pg_embedding /tmp
Enter fullscreen mode Exit fullscreen mode

Step 2: Navigate to the pg_embedding directory:

cd /tmp/pg_embedding
Enter fullscreen mode Exit fullscreen mode

Step 3: To ensure smooth compatibility with your PostgreSQL version, install the required dependencies:

sudo apt install postgresql-server-dev-XX
Enter fullscreen mode Exit fullscreen mode

(Replace XX with your PostgreSQL version. For example if you're using Postgres 15.3 use 15)

Step 4: Next, build and install the project using make command:

make
sudo make install
Enter fullscreen mode Exit fullscreen mode

Step 5: Now, let's add the extension using psql.
Note: You need login as superuser to be able to add extensions.

sudo su postgres # Login as super user
psql
Enter fullscreen mode Exit fullscreen mode

Step 6: Following command creates the extension

create extension embedding;
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the installation by checking the extensions with the \dx command and \q to quit psql.

If you'd like to add the extension to Postgres running on Docker,

Obtain the name or ID of your Postgres container:

docker ps
Enter fullscreen mode Exit fullscreen mode

Once you have it, let's dive into the container to do some shell scripting:

docker exec -it <NAME | ID> sh
Enter fullscreen mode Exit fullscreen mode

Voila! You've gained access to the Docker realm. Execute the same commands used on the mainland to work your magic within Docker.

But, heed this warning! 🚨 Your Docker installation may be hampered by missing additional packages like git, make, gcc, and g++. To sail smoothly, install them with:

sudo apt-get install git make gcc g++
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Clone the repo and copy the pg_embedding directory to docker container and skip git installation using:

docker cp pg_embedding <CONTAINER_ID>:/tmp
Enter fullscreen mode Exit fullscreen mode

Continue from Step 2 to finish the installation.

There you have it! 🌟 You've successfully installed the pg_embedding extension. Happy coding and may your endeavors be magical! 🧚‍♀️

Credits:
Photo by Sergey Pesterev on Unsplash

Top comments (0)