How to Set Up PG Vector on Ubuntu or Debian

Vadim Pryakhin
3 min readJul 5, 2024

--

In the age of machine learning and data science, efficient handling of high-dimensional vectors is crucial. It can help you with tasks such as recommendation systems, image recognition, and natural language processing. While there are many types of vector databases available, I prefer PostgreSQL because it can also be used for your other data, eliminating the need to set up different databases.

PG Vector is an extension for PostgreSQL that facilitates this by enabling fast similarity searches on vector data. This guide will walk you through setting up PG Vector on an Ubuntu or Debian system, ensuring you can leverage this powerful tool for your projects.

Prerequisites

Before you begin, ensure you have the following:

  • A system running Ubuntu or Debian
  • Root or sudo access to the system
  • Basic knowledge of PostgreSQL

Installing PostgreSQL

First, we need to install PostgreSQL. Follow these steps to get PostgreSQL up and running on your Ubuntu or Debian system.

  1. Update the Package List:
sudo apt update

2. Install PostgreSQL:

sudo apt install postgresql postgresql-contrib -y

3. Start and Enable PostgreSQL Service:

sudo systemctl start postgresql
sudo systemctl enable postgresql

4. Verify Installation:

psql --version

Installing PG Vector

Once PostgreSQL is installed, we can proceed to install the PG Vector extension. PG Vector is a PostgreSQL extension that allows efficient handling of vector data.

  1. Install Prerequisites:
sudo apt install build-essential postgresql-server-dev-all -y

2. Clone the PG Vector Repository:

git clone https://github.com/pgvector/pgvector.git
cd pgvector

3. Build and Install PG Vector:

make
sudo make install

Setting Up the User and Database

After installing PG Vector, the next step is to set up a PostgreSQL user and database to use the extension.

  1. Switch to the PostgreSQL User:
sudo -i -u postgres

2. Create a New Database User:

createuser --interactive

Follow the prompts to set the username and privileges.

3. Create a New Database:

createdb your_database_name

4. Access the PostgreSQL Shell:

psql

5. Grant Privileges to the New User:

GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;

6. Enable PG Vector Extension:

\c your_database_name
CREATE EXTENSION vector;

Conclusion

Setting up PG Vector on an Ubuntu or Debian system might seem daunting at first, but by following these steps, you’ll have everything up and running smoothly. With PostgreSQL and PG Vector, you can manage and query high-dimensional vector data efficiently, which is a game-changer for any data science or machine learning project.

Remember, you’re not just setting up a specialized vector database; you’re enhancing your existing PostgreSQL setup to handle complex vector operations. This dual-purpose functionality saves time and resources, making your data management more streamlined.

If you encounter any issues or have questions along the way, don’t hesitate to reach out to the PostgreSQL and PG Vector communities. They’re great resources full of knowledgeable people willing to help.

--

--