This repository contains a Docker Compose configuration for deploying PostgreSQL with pgvector extension, optimized for Dokploy deployment.
- PostgreSQL 16 with pgvector extension
- Persistent data storage using Docker volumes
- Health checks for monitoring
- Environment-based configuration
- Initialization scripts for database setup
- Ready for Dokploy deployment
-
Copy the environment file:
cp .env.example .env
-
Edit the
.envfile with your desired configuration:- Set a secure password for
POSTGRES_PASSWORD - Adjust database name and user if needed
- Change port mapping if required
- Set a secure password for
-
Run the container:
docker-compose up -d
| Variable | Default | Description |
|---|---|---|
POSTGRES_DB |
postgres |
Database name |
POSTGRES_USER |
postgres |
Database user |
POSTGRES_PASSWORD |
postgres |
Database password (change this!) |
POSTGRES_PORT |
5432 |
External port mapping |
- Dokploy instance running
- Git repository with this code pushed
- Environment variables configured in Dokploy
-
Push to Git Repository
- Ensure all files are committed to your Git repository
- Push to your preferred Git provider (GitHub, GitLab, etc.)
-
Create Application in Dokploy
- Log in to your Dokploy instance
- Click "New Application"
- Select "Git" as the source
- Connect your Git repository
- Select the branch containing this code
-
Configure Environment Variables
- In Dokploy, go to the "Environment" tab
- Add the following environment variables:
POSTGRES_DB=your_db_name POSTGRES_USER=your_username POSTGRES_PASSWORD=your_secure_password POSTGRES_PORT=5432
-
Set Resource Limits (Optional)
- Go to "Resources" tab
- Set appropriate CPU and memory limits based on your needs
-
Configure Persistent Storage (Optional)
- Go to "Volumes" tab
- Add a persistent volume for
/var/lib/postgresql/datato ensure data persistence
-
Deploy
- Click "Deploy" to start the deployment
- Wait for the deployment to complete
- Check logs to ensure PostgreSQL starts correctly
Once deployed, you can use pgvector in your SQL queries:
-- Create a table with vector column
CREATE TABLE documents (
id bigserial PRIMARY KEY,
content text,
embedding vector(1536)
);
-- Insert data with embeddings
INSERT INTO documents (content, embedding)
VALUES ('Hello world', '[0.1, 0.2, 0.3, ...]');
-- Perform similarity search
SELECT content, 1 - (embedding <=> '[0.1, 0.2, 0.3, ...]') AS similarity
FROM documents
ORDER BY embedding <=> '[0.1, 0.2, 0.3, ...]'
LIMIT 10;The container includes a health check that runs every 30 seconds:
- Test:
pg_isready -U postgres -d postgres - Timeout: 10 seconds
- Retries: 3
- Start period: 60 seconds
postgres_data: Persistent storage for PostgreSQL data files./init-scripts: Mounted to/docker-entrypoint-initdb.dfor initialization scripts
- Default:
5432:5432(host:container) - Configurable via
POSTGRES_PORTenvironment variable
- Host:
localhost(or your server IP) - Port:
5432(or configured port) - Database:
${POSTGRES_DB} - User:
${POSTGRES_USER} - Password:
${POSTGRES_PASSWORD}
Connection URL format:
postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${POSTGRES_PORT}/${POSTGRES_DB}
docker-compose exec postgres pg_dump -U postgres postgres > backup.sqldocker-compose exec -T postgres psql -U postgres postgres < backup.sqldocker-compose exec postgres psql -U postgres -d postgres- Always change the default password in production
- Consider using SSL connections in production
- Regularly update to the latest PostgreSQL version
- Use Dokploy's built-in networking features to secure database access
- Database not starting: Check logs for authentication errors
- Connection refused: Verify port mapping and firewall settings
- Data loss: Ensure persistent volumes are properly configured
- Performance issues: Monitor resource usage and adjust limits in Dokploy