🐘 Installing and Deploying PostgreSQL + pgvector in Docker #
This article shows how to quickly deploy a PostgreSQL database with the pgvector extension in a Docker Compose environment, so your local or development setup can support vector retrieval and AI applications (LangChain, RAG, semantic search, and the like).
📦 1. Prepare the Environment #
Make sure your system has:
- Docker
- Docker Compose
- A
.envfile with database environment variables, for example:
|
|
🧱 2. Create the Project Structure #
The recommended project layout:
|
|
⚙️ 3. Write the docker-compose.yml #
Use the official pgvector/pgvector image (based on PostgreSQL 16/17, with the pgvector extension built in):
|
|
🗃️ 4. Initialize the pgvector Extension #
Create the init.sql file:
|
|
This file runs automatically when the container first starts and the database initializes.
🚀 5. Start the Database #
|
|
Check container status:
|
|
When the status shows healthy, the database is up and running.
🔍 6. Verify pgvector Is Enabled #
Run the following to confirm the extension exists:
|
|
Example output:
|
|
🧠 7. A Quick Functional Test #
Create a simple table in the database and run a vector similarity search:
|
|
Example output:
|
|
This confirms pgvector is enabled and can execute vector searches.
✅ 8. Summary #
| Item | Value |
|---|---|
| Image | pgvector/pgvector:pg16 |
| Default port | 5432 |
| Data persistence directory | ./postgre |
| Initialization script | init.sql |
| Extension | pgvector |
With these steps you have successfully deployed PostgreSQL + pgvector in Docker. From here you can plug it straight into LangChain, LlamaIndex, or your own RAG application.