This guide explains how to install a PostgreSQL database using Docker and how to easily install it on Kubernetes using Helm. Necessary scripts for the installation will also be provided, which can be modified to suit your environment.
Prerequisites:
- Docker and Docker-Compose installed
- Access to a Kubernetes cluster
- Helm installed
Contents:
- Installation with Docker Compose:
- Install PostgreSQL locally using Docker.
- Installation on Kubernetes:
- Install PostgreSQL using Helm.
* Installing Locally Using Docker Compose
* In the example, the local path for storing data files is set to “/data/postgres.” To ensure compatibility, the group ID and user ID used by Docker must match the ownership of this data path. Therefore, creating the data path and setting the correct ownership should be done first.
* The Bitnami PostgreSQL image uses group ID 1001 and user ID 1001.
user:~$ sudo mkdir -p /data/postgres
user:~$ sudo chown 1001:1001 /data/postgres
docker-compose.yml
services:
# define postgresql container
postgresql:
image: docker.io/bitnami/postgresql:17
restart: unless-stopped
container_name: postgresql
ports:
- '5432:5432'
volumes:
- '/data/postgres:/bitnami/postgresql'
environment:
- POSTGRESQL_POSTGRES_PASSWORD=postgres
- POSTGRESQL_USERNAME=rsnet
- POSTGRESQL_PASSWORD=rsnet
- POSTGRESQL_DATABASE=rsnet
Commands to Start and Stop the Container
# start container
user:~$ docker compose up postgresql -d
# stop container
user:~$ docker container stop
# remove container
user:~$ docker container rm
* Installing Locally Using Docker Compose : vector extension
* To use vector database functionality in PostgreSQL, you need to use an image that includes the pgvector extension. If you wish to utilize the DockerHub image provided by RealStudy.NET, you can specify the image path as shown below.
services:
# define postgresql container
postgresql:
image: nockchun/postgresql_pgvector.rs:17.2.0_0.4.0
restart: unless-stopped
container_name: postgresql
ports:
- '5432:5432'
volumes:
- '/data/postgres:/bitnami/postgresql'
environment:
- POSTGRESQL_POSTGRES_PASSWORD=postgres
- POSTGRESQL_USERNAME=rsnet
- POSTGRESQL_PASSWORD=rsnet
- POSTGRESQL_DATABASE=rsnet
* You need to enter the PostgreSQL container shell and register (load) the vector DB extension. Then, restart the container.
# Run the container shell
user:~$ docker compose exec postgresql bash
# Loading vector extension
container:/$ psql -U postgres -c 'ALTER SYSTEM SET shared_preload_libraries = "vectors.so"'
container:/$ psql -U postgres -c 'ALTER SYSTEM SET search_path TO "$user", public, vectors'
# Restart container
user:~$ docker compose restart postgresql
* Activate the vector DB extension using a query tool like psql or DBeaver.
# Enable the extension
psql:~$ CREATE EXTENSION vectors;
# Disable the extension
psql:/$ DROP EXTENSION IF EXISTS vectors;
* Installing on Kubernetes Using Helm
Utilizing Helm in a Kubernetes environment allows for a straightforward and efficient installation and management of PostgreSQL. This method enables completion of major configurations with a single command, making it accessible even for beginners. The guide uses Bitnami’s Helm Chart and Docker image for the simplest installation method.
Add the Bitnami Repository:
user:~$ helm repo add bitnami https://charts.bitnami.com/bitnami
user:~$ helm repo update
Check Configuration Options:
user:~$ helm show values bitnami/postgresql > postgresql-values.yaml
* This command saves the values.yaml file, which contains configurable options and their exact locations. Alternatively, you can refer to the official documentation on Artifact Hub.
* Artifact Hub: https://artifacthub.io/packages/helm/bitnami/postgresql
Installation Command:
user:~$ helm install postgresql bitnami/postgresql --namespace postgresql --create-namespace \
--set global.postgresql.auth.postgresPassword=postgres \
--set global.postgresql.auth.username=rsnet \
--set global.postgresql.auth.password=rsnet \
--set global.postgresql.auth.database=rsnet \
--set primary.persistence.storageClass=local-path \
--set primary.persistence.size=50Gi \
--set primary.resources.requests.memory=512Mi \
--set primary.resources.limits.memory=30000Mi \
--set primary.service.type=LoadBalancer \
--set primary.service.port=5432
* To use vector DB features in PostgreSQL, specify an image that includes the pgvector extension. If you prefer to use the DockerHub image provided by RealStudy.NET, set the image.repository and image.tag as follows.
user:~$ helm install postgresql bitnami/postgresql --namespace postgresql --create-namespace \
--set image.repository=nockchun/postgresql_pgvector.rs \
--set image.tag=17.2.0_0.4.0 \
--set global.postgresql.auth.postgresPassword=postgres \
--set global.postgresql.auth.username=rsnet \
--set global.postgresql.auth.password=rsnet \
--set global.postgresql.auth.database=rsnet \
--set primary.persistence.storageClass=local-path \
--set primary.persistence.size=50Gi \
--set primary.resources.requests.memory=512Mi \
--set primary.resources.limits.memory=30000Mi \
--set primary.service.type=LoadBalancer \
--set primary.service.port=5432
* If you are using an image that includes the pgvector extension, you need to register (load) the vector DB extension after installation as explained in the section “Using Docker Compose Locally: Using the Vector Extension.” Then, activate it using a query tool such as psql or DBeaver.
Verify Installation:
# Check if it is installed and running properly.
user:~$ kubectl get all -n postgresql