PGVector on CloudSQL for GCP

Adham Sersour
1 min read
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.

Infrastructure as Code

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.

Vector Search Context: Vector search represents a specialized approach to storing and querying high-dimensional vectors efficiently. When working with embeddings for recommendation or RAG applications, having stable and reliable infrastructure can be particularly important.

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:

mermaid
100%

Comprehensive architecture showing repository structure, workflow, and GCP resources

Consistency
Speed
Version Control
Scalability
Reproducibility
Documentation
Cost Control

Team Experience Varies:

Some teams have reported significant time savings and reduced incidents after adopting Infrastructure as Code approaches, though results vary based on team expertise and specific use cases.

โœจ Key Features

Our PGVector on CloudSQL solution provides several key advantages:

Infrastructure as Code
Terraform-based provisioning of Google Cloud SQL PostgreSQL instances with pgvector extension
Simplified Operations
All database operations managed directly through intuitive Makefile commands
Multi-Environment Support
Separate configurations for development, pre-production, and production environments
Quick Setup
Easy-to-follow setup process with comprehensive documentation and examples

๐Ÿ—๏ธ 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 recommendations
mermaid
100%

Visual overview of the repository structure and modular organization for Terraform-based PGVector deployment

The architecture aims to be straightforward while addressing common infrastructure needs:

๐Ÿ๏ธ Dedicated GCP project: created by Terraform providing isolation between projects
๐Ÿ”’ Custom VPC: with private network connectivity for enhanced security
๐ŸŒ Cloud SQL PostgreSQL instance: with configurable access options (public access limited to dev environments)
๐Ÿงฉ Vector database configuration: for storing embeddings
๐Ÿšฆ Environment separation: for dev, preprod, and prod following common DevOps practices

๐Ÿ“Š What Does the Terraform Setup Do?

The modular configuration in terraform-sql automates the following:

๐Ÿ—๏ธ Project Module Configuration

Initializes your Google Cloud project: and authenticates Terraform โ˜๏ธ
Activates essential APIs: Cloud SQL, networking, IAM, Compute, and more ๐Ÿ”Œ
Creates service accounts: with proper permissions ๐Ÿ”‘

๐ŸŒ VPC Module Configuration

Creates a secure VPC network: with private subnet ranges ๐Ÿ›ก๏ธ
Configures firewall rules: for secure communication ๐Ÿ”ฅ
Sets up Private Service Access: for Cloud SQL connections ๐ŸŒ

๐Ÿ—„๏ธ SQL Module Configuration

Provisions a Cloud SQL PostgreSQL instance: with proper sizing based on environment ๐Ÿ“Š
Configures it for the pgvector extension: enabling vector similarity search ๐Ÿง 
Sets up database users: with secure passwords ๐Ÿ‘ค
Configures automated backups: and high availability options ๐Ÿ’พ

๐Ÿงช Environment-Specific Configurations

Separate configurations: for dev, preprod, and prod environments ๐Ÿงช
Parameterization through variables: for project name, region, database size, and more ๐Ÿงฎ
Uses Terraform workspaces: to isolate state between environments ๐Ÿ“‹

๐Ÿ”— Outputs and Integration

Provides connection details: and resource IDs for use in your application or CI/CD pipelines ๐Ÿ”—
Outputs important information: like database IP, connection names, and project details ๐Ÿ“

๐Ÿ” 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

Environment-specific security settings: dev is more open, prod is locked down
Parameterized authorized networks: for database access, not hard-coded
Private Service Access for Cloud SQL: ensuring database isn't publicly exposed in production
IAM role separation: with principle of least privilege
Separate service accounts: for different components

๐Ÿšจ Production Checklist:

  • ๐ŸŽฏ Set authorized_networks to 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

  1. 1make tf-init ENV=dev # Initialize Terraform for your chosen environment
  2. 2make tf-plan ENV=dev # Review what will be created
  3. 3make tf-apply ENV=dev # Apply the changes

๐Ÿ—„๏ธ Managing Your Database

  1. 1make setup-db ENV=dev # Create database schema with pgvector extension
  2. 2make add-sample-data ENV=dev # Add sample product data
  3. 3make check-db ENV=dev # Verify database setup
  4. 4make run-simple-demo ENV=dev # Run simple demo queries

๐Ÿ”ง Environment Management

  1. 1make env # Check current environment settings
  2. 2make connect-db ENV=dev # Connect directly to the database
  3. 3make tf-destroy ENV=dev # Clean up when done
Tip: This Makefile approach simplifies complex commands, helping team members collaborate more effectively without needing to memorize every technical detail.

๐Ÿง‘โ€๐Ÿ’ป 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=dev

This 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);
๐Ÿ”ฎ Advanced Tip: You can adjust the vector dimension (1536 here is for OpenAI's text-embedding-ada-002) based on your embedding model of choice. All this is handled through the Makefile, so you don't need to remember complex SQL commands.

๐Ÿค” Potential Use Cases

This infrastructure can support various vector-based applications:

Use CaseDescriptionImplementation Benefits
๐Ÿ›’ Product RecommendationsDeliver personalized suggestions using semantic similarityHigher conversion rates, improved UX
๐Ÿค– Retrieval-Augmented Generation (RAG)Enhance chatbots and LLMs with context-aware retrievalMore accurate responses, reduced hallucinations
๐Ÿ” Semantic SearchGo beyond keywords to find relevant documents, products, or answersBetter search relevance, natural language queries
๐Ÿงน Content DeduplicationIdentify near-duplicate items in large datasetsData quality improvement, storage optimization
๐Ÿšจ Anomaly DetectionSpot unusual patterns by comparing vector representationsEarly threat detection, quality assurance
๐ŸŽฏ Personalization EnginesTailor user experiences based on semantic profilesIncreased 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?

Semantic Understanding: Products were matched based on meaning, not just keywords
Real-time Performance: Sub-100ms query responses enabled dynamic recommendations
Scalable Infrastructure: Terraform automation allowed rapid scaling during peak seasons
A/B Testing: Multiple environments enabled safe experimentation

๐Ÿงฌ 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:

Create similar testing environments more efficiently
Deploy to different regions using the same configuration base
Recover from accidental resource deletions by reapplying your configuration
Onboard new team members by sharing code rather than lengthy setup instructions

๐Ÿ† 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

EnvironmentAccessNetworkResourcesAvailabilityProtection
๐Ÿงช devPublic IPOpen firewallLower specsZONALNo deletion protection
๐Ÿ”„ preprodPrivate IP onlyVPC networkMedium specsZONALNo deletion protection
๐Ÿญ prodPrivate IP onlyStrict VPCHigher specsREGIONALDeletion protection + backups

Deployment Steps

1. Environment progression (follow the path to production):

  • โ€ข Start with dev environment to test your configuration ๐Ÿงช
  • โ€ข Validate in preprod with production-like settings ๐Ÿ”
  • โ€ข Apply strict security rules only in prod ๐Ÿ›ก๏ธ

2. Secure your database (because security matters):

  • โ€ข Restrict authorized_networks to 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 .gitignore for .tfvars files ๐Ÿšซ
  • โ€ข Use environment variables for sensitive data in CI/CD pipelines ๐Ÿ”‘
  • โ€ข Consider using google_secret_manager_secret resource ๐Ÿ—๏ธ

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:

๐ŸŒ Environment Separation: Different configurations for dev, preprod, and prod environments
๐Ÿงฉ Modular Design: Reusable modules for various infrastructure components
๐Ÿ›ก๏ธ Progressive Security: Security controls that adapt to each environment
โš™๏ธ Operation Helpers: Makefile commands to assist with common tasks
๐Ÿ“ Organized Structure: A folder organization that aims to be logical and maintainable
๐Ÿ’ก Interested in trying this approach? Feel free to explore the repo and experiment with the commandsโ€”we'd love to hear about your experience and how you adapt it to your specific needs.

๐Ÿ“š Additional Resources

๐Ÿ“ Coming Next: "Vector Database Applications" - Where we'll explore practical implementations like recommendation systems, retrieval-augmented generation (RAG), semantic search, and other real-world use cases.

We'd welcome your feedback, suggestions, or contributions to help improve this project.

React:

Comments

No comments yet. Be the first to comment!