PGVector on CloudSQL for GCP

Automate PGVector on Cloud SQL deployment using Terraform and Makefiles! Deploy a complete vector search infrastructure with single commands; discover how. Build powerful semantic apps faster and easier, saving time and reducing errors.
A practical guide to automating vector search infrastructure with Terraform and Makefiles, streamlining the deployment of PGVector on Cloud SQL and letting you focus on building powerful semantic applications.
๐ View on GitHub๐ Why Infrastructure as Code May Help with Vector Search
Manual cloud setup can sometimes be time-consuming and prone to inconsistenciesโa missed configuration step might lead to unexpected behavior or security concerns. When building a vector search application, a RAG system, or a semantic recommendation engine for production, having a reliable infrastructure approach alongside your application code could be beneficial.
Many of us have experienced the initial satisfaction of setting up resources through the Google Cloud Console, only to later struggle when trying to recreate that environment or share the exact setup with teammates. Without version control or documentation, reproducing environments consistently can be challenging.
We've found that combining Terraform for infrastructure definition with Makefiles for operation simplification might help address some common challenges. This approach allows you to define your cloud environment as code and potentially automate deployments.
By turning your vector database stack into code, you get:
Comprehensive architecture showing repository structure, workflow, and GCP resources
Team Experience Varies:
โจ Key Features
Our PGVector on CloudSQL solution provides several key advantages:
๐๏ธ Terraform Architecture for PostgreSQL Vector Search
Ready to build something cool? Let's get our hands dirty with a complete PostgreSQL setup that has pgvector extensions baked in. No more manual steps or forgotten configurationsโjust pure, repeatable infrastructure.
The repository is organized using a modular approach with separate environments, making it enterprise-ready from day one:
Repository Structure
.
โโโ ๐ Makefile # Main command interface for all operations
โโโ ๐ terraform-sql/ # Terraform configuration for Cloud SQL
โ โโโ ๐ environments/ # Environment-specific configurations
โ โ โโโ ๐ dev/ # Development environment
โ โ โโโ ๐ preprod/ # Pre-production environment
โ โ โโโ ๐ prod/ # Production environment
โ โโโ ๐ modules/ # Reusable Terraform modules
โ โโโ ๐ project/ # GCP project configuration
โ โโโ ๐ sql/ # Cloud SQL instance configuration
โ โโโ ๐ vpc/ # Network configuration
โโโ ๐ how_to_tutorials/ # Example applications and demos
โโโ ๐ semantic_demo/ # Semantic search demo for product recommendationsVisual overview of the repository structure and modular organization for Terraform-based PGVector deployment
The architecture aims to be straightforward while addressing common infrastructure needs:
๐ What Does the Terraform Setup Do?
The modular configuration in terraform-sql automates the following:
๐๏ธ Project Module Configuration
๐ VPC Module Configuration
๐๏ธ SQL Module Configuration
๐งช Environment-Specific Configurations
๐ Outputs and Integration
๐ Security Considerations
The repository implements different security configurations for each environment. Let's look at the networking module which includes thoughtful firewall rules:
Firewall Configuration Example
# Allow PostgreSQL access from authorized sources
resource "google_compute_firewall" "allow_postgres" {
name = "${var.name_prefix}-allow-postgres"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "tcp"
ports = ["5432"]
}
# In development, this might be more permissive
# In production, this should be limited to specific sources
source_ranges = var.authorized_networks
target_tags = ["cloudsql"]
description = "Allow PostgreSQL access from authorized networks"
}
# Allow internal traffic within the VPC
resource "google_compute_firewall" "allow_internal" {
name = "${var.name_prefix}-allow-internal"
network = google_compute_network.vpc_network.self_link
allow {
protocol = "all"
}
source_ranges = [var.subnet_cidr]
description = "Allow all internal traffic within the VPC"
}๐ Key Security Features
๐จ Production Checklist:
- ๐ฏ Set
authorized_networksto specific IP ranges in prod environment - ๐ค Enable Cloud SQL IAM authentication for user access
- ๐๏ธ Store secrets in Secret Manager, not in Terraform variables
- ๐ Enable audit logging and monitoring
- ๐ Implement database encryption at rest and in transit
The repository suggests a progressive security model that allows starting with more permissive settings in development environments while implementing stricter controls in production. We recommend carefully reviewing security settings before deploying to production.
๐ฆ Simplified Deployment: Using the Makefile
One of the standout features of this project is its streamlined approach using Makefiles. You can deploy and manage your entire vector search infrastructure with simple commands:
โ๏ธ Setting Up Your Environment
# Copy the example environment file cp .env.example .env # Edit with your credentials # Copy the example variables file cp terraform-sql/environments/dev/terraform.tfvars.example terraform-sql/environments/dev/terraform.tfvars # Edit terraform.tfvars with your specific configuration values
๐ Deploying Infrastructure
- 1make tf-init ENV=dev # Initialize Terraform for your chosen environment
- 2make tf-plan ENV=dev # Review what will be created
- 3make tf-apply ENV=dev # Apply the changes
๐๏ธ Managing Your Database
- 1make setup-db ENV=dev # Create database schema with pgvector extension
- 2make add-sample-data ENV=dev # Add sample product data
- 3make check-db ENV=dev # Verify database setup
- 4make run-simple-demo ENV=dev # Run simple demo queries
๐ง Environment Management
- 1make env # Check current environment settings
- 2make connect-db ENV=dev # Connect directly to the database
- 3make tf-destroy ENV=dev # Clean up when done
๐งโ๐ป Post-Deployment: Setting Up pgvector
After your infrastructure is deployed, you need to set up the vector extension. Traditionally this would require multiple manual steps, but our Makefile simplifies this:
make setup-db ENV=devThis single command connects to your database and runs:
SQL Setup Script
CREATE EXTENSION IF NOT EXISTS vector;
-- Create a database for our application
CREATE DATABASE vector_search;
\c vector_search
-- Create a table for semantic product data
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
category TEXT,
price DECIMAL(10, 2),
embedding VECTOR(1536) -- For OpenAI embeddings, adjust as needed
);
-- Create an index for fast similarity search
CREATE INDEX ON products USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);๐ค Potential Use Cases
This infrastructure can support various vector-based applications:
| Use Case | Description | Implementation Benefits |
|---|---|---|
| ๐ Product Recommendations | Deliver personalized suggestions using semantic similarity | Higher conversion rates, improved UX |
| ๐ค Retrieval-Augmented Generation (RAG) | Enhance chatbots and LLMs with context-aware retrieval | More accurate responses, reduced hallucinations |
| ๐ Semantic Search | Go beyond keywords to find relevant documents, products, or answers | Better search relevance, natural language queries |
| ๐งน Content Deduplication | Identify near-duplicate items in large datasets | Data quality improvement, storage optimization |
| ๐จ Anomaly Detection | Spot unusual patterns by comparing vector representations | Early threat detection, quality assurance |
| ๐ฏ Personalization Engines | Tailor user experiences based on semantic profiles | Increased engagement, user satisfaction |
๐ Success Story: E-commerce Recommendation Engine
Company: A mid-sized e-commerce platform โ implemented vector-based product recommendations using similar infrastructure, resulting in a 35% increase in click-through rates and 20% boost in sales conversion.
๐ What made the difference?
๐งฌ The Power of Reproducibility
One potential benefit of Infrastructure as Code is improved reproducibility. With the Terraform and Makefile approach, you might find it easier to:
๐ Production-Ready Deployment Tips
Ready to go beyond the demo and into production? Here are some hard-learned lessons to save you some late-night troubleshooting sessions:
Environment Differences
| Environment | Access | Network | Resources | Availability | Protection |
|---|---|---|---|---|---|
| ๐งช dev | Public IP | Open firewall | Lower specs | ZONAL | No deletion protection |
| ๐ preprod | Private IP only | VPC network | Medium specs | ZONAL | No deletion protection |
| ๐ญ prod | Private IP only | Strict VPC | Higher specs | REGIONAL | Deletion protection + backups |
Deployment Steps
1. Environment progression (follow the path to production):
- โข Start with
devenvironment to test your configuration ๐งช - โข Validate in
preprodwith production-like settings ๐ - โข Apply strict security rules only in
prod๐ก๏ธ
2. Secure your database (because security matters):
- โข Restrict
authorized_networksto specific IP ranges ๐ - โข Enable Private Service Connect for production deployments ๐
- โข Enable Cloud SQL IAM authentication by setting
enable_iam_auth = true๐ค
3. Optimize for performance (vector search gets hungry):
- โข Use appropriate instance sizes:
db-f1-microโdb-custom-4-15360 - โข Vector search loves RAMโuse the high-memory options ๐ง
- โข Benchmark with real data in preprod before going to production โณ
4. Manage secrets properly (no passwords in GitHub, please):
- โข The repository includes
.gitignorefor.tfvarsfiles ๐ซ - โข Use environment variables for sensitive data in CI/CD pipelines ๐
- โข Consider using
google_secret_manager_secretresource ๐๏ธ
5. Monitoring saves lives (or at least your weekend):
- โข Set up Cloud Monitoring alerts before you need them ๐จ
- โข Enable automated backups using the
backup_configuration๐พ - โข Consider read replicas for high-traffic applications ๐
๐ Conclusion
By automating your vector search infrastructure with Terraform and Makefiles, you might find your workflow becoming more streamlined and reproducible. Rather than clicking through console interfaces and trying to remember all the steps, this approach offers a way to document and version your infrastructure setup.
๐ฏ Key Project Features
The pgvector_cloudsql_gcp repository implements this approach with features like:
๐ Additional Resources
We'd welcome your feedback, suggestions, or contributions to help improve this project.
Comments
No comments yet. Be the first to comment!