Skip to content

Commit 776509b

Browse files
committed
feat: getting started with pgvector
Signed-off-by: Gabriele Bartolini <[email protected]>
1 parent c2ca959 commit 776509b

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
302 KB
Loading
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: "CNPG Recipe 18 - Getting Started with pgvector on Kubernetes Using CloudNativePG"
3+
date: 2025-06-05T22:42:04+02:00
4+
description: "Set up a PostgreSQL cluster with `pgvector` on Kubernetes using CloudNativePG in a fully declarative and streamlined way"
5+
tags: ["postgresql", "postgres", "kubernetes", "k8s", "cloudnativepg", "cnpg", "postgresql", "postgres", "dok", "data on kubernetes", "pgvector", "database"]
6+
cover: cover.jpg
7+
thumb: thumb.jpg
8+
draft: false
9+
---
10+
11+
_Learn how to set up a PostgreSQL cluster with the `pgvector` extension on
12+
Kubernetes using CloudNativePG—all in a fully declarative way. This article
13+
walks you through the process in just a few minutes, from cluster creation to
14+
extension installation._
15+
16+
<!--more-->
17+
18+
---
19+
20+
21+
[`pgvector`](https://github.com/pgvector/pgvector) has quickly become one of the
22+
most popular PostgreSQL extensions, especially in the context of AI and machine
23+
learning. It introduces native support for vector data types, which are
24+
essential for similarity search, embedding storage, and other AI-driven use
25+
cases.
26+
27+
In this article, I’ll walk you through how to create a PostgreSQL cluster with
28+
`pgvector` support in Kubernetes using [CloudNativePG](https://cloudnative-pg.io/).
29+
As always, we’ll take a fully declarative approach—and it’ll only take a few
30+
minutes.
31+
32+
## Prerequisites
33+
34+
Before you begin, make sure you have a local Kubernetes environment up and
35+
running using [Kind](https://kind.sigs.k8s.io/) and that you’ve installed the
36+
latest version of CloudNativePG. If you haven’t yet, follow the steps in
37+
["CloudNativePG Recipe 1 - Setting up your local playground in minutes"]({{< relref "../20240303-recipe-local-setup/index.md" >}})
38+
to get everything ready.
39+
40+
## Step 1: Define the PostgreSQL Cluster
41+
42+
Let’s start by creating a simple, single-instance PostgreSQL cluster named
43+
`pgvector`. You can scale it later depending on your needs.
44+
45+
Create a file named `pgvector.yaml` with the following contents:
46+
47+
```yaml
48+
{{< include "yaml/pgvector.yaml" >}}
49+
```
50+
51+
Then apply it with:
52+
53+
```sh
54+
kubectl apply -f pgvector.yaml
55+
```
56+
57+
## Step 2: Check Cluster Status
58+
59+
Once the cluster is being created, you can monitor its status with the `cnpg`
60+
plugin:
61+
62+
```sh
63+
kubectl cnpg status pgvector
64+
```
65+
66+
## Step 3: Install the `pgvector` Extension
67+
68+
By default, CloudNativePG creates a database and a user both named `app` for
69+
applications (see ["CNPG Recipe 2 - Inspecting Default Resources in a CloudNativePG Cluster"]({{< relref "../20240307-recipe-inspection/index.md" >}})
70+
for more information). CloudNativePG also supports a powerful feature: you can
71+
declaratively [define a `Database` resource](https://cloudnative-pg.io/documentation/current/declarative_database_management/)
72+
and specify extensions to be installed in it.
73+
74+
Since `pgvector` is already bundled in the default
75+
[operand container image for PostgreSQL](https://github.com/cloudnative-pg/postgres-containers),
76+
installing it is straightforward. Create a new file named
77+
`pgvector-db.yaml` with the following contents:
78+
79+
```yaml
80+
{{< include "yaml/pgvector-db.yaml" >}}
81+
```
82+
83+
Apply it with:
84+
85+
```sh
86+
kubectl apply -f pgvector-db.yaml
87+
```
88+
89+
You can inspect the new `Database` resource using:
90+
91+
```sh
92+
kubectl get database pgvector-app
93+
```
94+
95+
or:
96+
97+
```sh
98+
kubectl describe database pgvector-app
99+
```
100+
101+
The controller for the `Database` resource transparently manages `CREATE
102+
EXTENSION`, as well as related commands like `ALTER EXTENSION` and `DROP
103+
EXTENSION`, when the resource is modified or deleted.
104+
105+
## Step 4: Verify the Extension
106+
107+
Let’s now connect to the `app` database and confirm that the `vector` extension
108+
is installed:
109+
110+
```sh
111+
kubectl cnpg psql pgvector -- app -c '\dx'
112+
```
113+
114+
You should see output similar to this:
115+
116+
```console
117+
List of installed extensions
118+
Name | Version | Schema | Description
119+
---------+---------+------------+------------------------------------------------------
120+
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
121+
vector | 0.8.0 | public | vector data type and ivfflat and hnsw access methods
122+
(2 rows)
123+
```
124+
125+
## You're All Set
126+
127+
And there you have it—`pgvector` is installed and ready to use in your `app`
128+
database, running inside a PostgreSQL cluster managed by CloudNativePG on
129+
Kubernetes. What you build with it is entirely up to you. If you're just
130+
getting started and want to experiment, head over to the project’s [“Getting Started” guide](https://github.com/pgvector/pgvector?tab=readme-ov-file#getting-started)
131+
for some practical examples.
132+
133+
This setup offers a solid foundation for experimenting with vector-based AI
134+
workloads using Postgres in a cloud-native way.
135+
136+
---
137+
138+
Stay tuned for the upcoming recipes! For the latest updates, consider
139+
subscribing to my [LinkedIn](https://www.linkedin.com/in/gbartolini/) and
140+
[Twitter](https://twitter.com/_GBartolini_) channels.
141+
142+
If you found this article informative, feel free to share it within your
143+
network on social media using the provided links below. Your support is
144+
immensely appreciated!
145+
146+
_Cover Picture: [“It is Elephants“](https://pxhere.com/en/photo/1604154)._
147+
113 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: postgresql.cnpg.io/v1
2+
kind: Database
3+
metadata:
4+
name: pgvector-app
5+
spec:
6+
name: app
7+
owner: app
8+
cluster:
9+
name: pgvector
10+
extensions:
11+
- name: vector
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: postgresql.cnpg.io/v1
2+
kind: Cluster
3+
metadata:
4+
name: pgvector
5+
spec:
6+
instances: 1
7+
8+
storage:
9+
size: 1Gi

0 commit comments

Comments
 (0)