Google Antigravity + PostgreSQL: Complete Database Integration Guide with MCP
N
Nick
December 19, 2025
👁️ 76 views
Google Antigravity + PostgreSQL: Complete Database Integration Guide with MCP
One of Google Antigravity's most powerful features is direct database integration through MCP (Model Context Protocol) servers. This guide shows you how to connect PostgreSQL to Antigravity for AI-powered database development.
Why Integrate PostgreSQL with Antigravity?
When you connect PostgreSQL to Google Antigravity, Gemini 3 can:
Query your database directly - Ask questions in natural language
Generate SQL - Create complex queries from descriptions
Understand your schema - AI knows your tables and relationships
You: "Find the top 10 users by post count with their average comment count per post"
Gemini 3 generates:
WITH user_stats AS (
SELECT
u.id,
u.email,
COUNT(DISTINCT p.id) as post_count,
COUNT(c.id) as total_comments
FROM users u
LEFT JOIN posts p ON p.author_id = u.id
LEFT JOIN comments c ON c.post_id = p.id
GROUP BY u.id, u.email
)
SELECT
email,
post_count,
ROUND(total_comments::numeric / NULLIF(post_count, 0), 2) as avg_comments_per_post
FROM user_stats
WHERE post_count > 0
ORDER BY post_count DESC
LIMIT 10;
Database Migrations with Antigravity
Generating Migrations
You: "Create a migration to add a tags table with a many-to-many relationship to posts"
Gemini 3 generates:
-- Migration: create_tags_system
-- Created: 2025-01-15
-- Create tags table
CREATE TABLE tags (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(50) NOT NULL UNIQUE,
slug VARCHAR(50) NOT NULL UNIQUE,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Create junction table for posts-tags
CREATE TABLE post_tags (
post_id UUID REFERENCES posts(id) ON DELETE CASCADE,
tag_id UUID REFERENCES tags(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (post_id, tag_id)
);
-- Create indexes for performance
CREATE INDEX idx_post_tags_post_id ON post_tags(post_id);
CREATE INDEX idx_post_tags_tag_id ON post_tags(tag_id);
CREATE INDEX idx_tags_slug ON tags(slug);
Rolling Back Migrations
You: "Generate the rollback for the tags migration"
-- Rollback: create_tags_system
DROP INDEX IF EXISTS idx_tags_slug;
DROP INDEX IF EXISTS idx_post_tags_tag_id;
DROP INDEX IF EXISTS idx_post_tags_post_id;
DROP TABLE IF EXISTS post_tags;
DROP TABLE IF EXISTS tags;
Generating TypeScript Types
From Database Schema
You: "Generate TypeScript interfaces for my database tables"
SELECT * FROM posts
WHERE author_id IN (SELECT id FROM users WHERE created_at > '2024-01-01')
Gemini 3 analysis:
-- Original query uses subquery - can be slow
-- Optimized with JOIN:
SELECT p.*
FROM posts p
INNER JOIN users u ON p.author_id = u.id
WHERE u.created_at > '2024-01-01';
-- If you need this often, add a partial index:
CREATE INDEX idx_posts_recent_authors
ON posts(author_id)
WHERE author_id IN (
SELECT id FROM users WHERE created_at > '2024-01-01'
);
-- Or better, use a materialized view for complex aggregations
EXPLAIN Analysis
You: "Run EXPLAIN ANALYZE on my query"
EXPLAIN ANALYZE
SELECT p.*, u.email, COUNT(c.id) as comment_count
FROM posts p
JOIN users u ON p.author_id = u.id
LEFT JOIN comments c ON c.post_id = p.id
WHERE p.published = true
GROUP BY p.id, u.email;
-- Gemini 3 interprets the results:
-- "The query performs a sequential scan on posts (slow for large tables).
-- Adding an index on posts(published) where published = true would help.
-- The GROUP BY is causing a Sort operation - consider adding an index
-- on comments(post_id) to speed up the join."
Row Level Security (RLS) Policies
Generating RLS Policies
You: "Create RLS policies so users can only see their own data"
-- Enable RLS on tables
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;
ALTER TABLE comments ENABLE ROW LEVEL SECURITY;
-- Posts policies
CREATE POLICY "Users can view published posts"
ON posts FOR SELECT
USING (published = true);
CREATE POLICY "Users can view their own drafts"
ON posts FOR SELECT
USING (auth.uid() = author_id);
CREATE POLICY "Users can insert their own posts"
ON posts FOR INSERT
WITH CHECK (auth.uid() = author_id);
CREATE POLICY "Users can update their own posts"
ON posts FOR UPDATE
USING (auth.uid() = author_id);
CREATE POLICY "Users can delete their own posts"
ON posts FOR DELETE
USING (auth.uid() = author_id);
-- Comments policies
CREATE POLICY "Anyone can view comments"
ON comments FOR SELECT
USING (true);
CREATE POLICY "Authenticated users can comment"
ON comments FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can edit their own comments"
ON comments FOR UPDATE
USING (auth.uid() = user_id);
CREATE USER mcp_reader WITH PASSWORD 'secure_password';
GRANT CONNECT ON DATABASE mydb TO mcp_reader;
GRANT USAGE ON SCHEMA public TO mcp_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_reader;
# Database Context
## Schema Overview
- users: Core user accounts
- profiles: Extended user data
- posts: User-generated content
- comments: Post discussions
## Conventions
- All tables use UUID primary keys
- Timestamps are TIMESTAMPTZ
- Soft deletes use deleted_at column
- RLS is enabled on all user-data tables
## Common Queries
- Get user with profile: JOIN users and profiles
- Get post feed: published = true, ORDER BY created_at DESC
Conclusion
Integrating PostgreSQL with Google Antigravity transforms database development. With Gemini 3 understanding your schema, you can: