|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## CRITICAL INSTRUCTION: PostgreSQL Version Restriction |
| 6 | + |
| 7 | +**ALWAYS USE POSTGRESQL 17 ONLY** |
| 8 | + |
| 9 | +- When starting servers, ONLY use: `nix run .#start-server 17` |
| 10 | +- When building, ONLY target PostgreSQL 17 |
| 11 | +- When testing, ONLY use: `nix build .#checks.psql_17 -L` |
| 12 | +- When building AMIs, ONLY use: `nix run .#build-test-ami 17` |
| 13 | +- NEVER use PostgreSQL 15 or OrioleDB 17 variants |
| 14 | +- If asked to work with other versions, politely decline and explain you must only work with PostgreSQL 17 |
| 15 | + |
| 16 | +## Project Overview |
| 17 | + |
| 18 | +This is **Supabase's PostgreSQL distribution** - an enhanced PostgreSQL build with 40+ extensions across three major versions (PostgreSQL 15, 17, and OrioleDB 17). The project uses Nix as the primary build system for reproducible builds and provides Docker/Packer alternatives for different deployment scenarios. |
| 19 | + |
| 20 | +## Development Commands |
| 21 | + |
| 22 | +### Primary Development Environment (Nix) |
| 23 | +```bash |
| 24 | +# Enter development environment |
| 25 | +nix develop |
| 26 | + |
| 27 | +# Start PostgreSQL server (ONLY use version 17) |
| 28 | +nix run .#start-server 17 |
| 29 | + |
| 30 | +# Connect client with migrations applied |
| 31 | +nix run .#start-client |
| 32 | + |
| 33 | +# Run database migrations |
| 34 | +nix run .#dbmate-tool |
| 35 | + |
| 36 | +# Run all tests |
| 37 | +nix flake check |
| 38 | + |
| 39 | +# Run tests for PostgreSQL 17 ONLY |
| 40 | +nix build .#checks.psql_17 -L |
| 41 | +``` |
| 42 | + |
| 43 | +### CI/CD and AMI Building |
| 44 | +```bash |
| 45 | +# Trigger Nix build |
| 46 | +nix run .#trigger-nix-build |
| 47 | + |
| 48 | +# Build test AMI (PostgreSQL 17 ONLY) |
| 49 | +nix run .#build-test-ami 17 |
| 50 | + |
| 51 | +# Run infrastructure tests |
| 52 | +nix run .#run-testinfra |
| 53 | + |
| 54 | +# Cleanup AMI |
| 55 | +nix run .#cleanup-ami |
| 56 | +``` |
| 57 | + |
| 58 | +### Legacy Build System |
| 59 | +```bash |
| 60 | +# Packer-based image building |
| 61 | +make init |
| 62 | +make output-cloudimg/packer-cloudimg |
| 63 | +make alpine-image |
| 64 | +``` |
| 65 | + |
| 66 | +## Architecture Overview |
| 67 | + |
| 68 | +### PostgreSQL Version Support |
| 69 | +- **PostgreSQL 17**: The ONLY version to use for all development and builds |
| 70 | +- Other versions (15, OrioleDB 17) exist in the codebase but MUST NOT be used |
| 71 | + |
| 72 | +Extension configuration is in `nix/postgresql/` but ONLY work with PostgreSQL 17. |
| 73 | + |
| 74 | +### Build System Architecture |
| 75 | +- **Primary**: Nix flakes (`flake.nix`) for reproducible builds |
| 76 | +- **Fallback**: Docker containers for development |
| 77 | +- **Production**: Packer + Ansible for AMI creation |
| 78 | + |
| 79 | +### Extension Management |
| 80 | +Extensions are organized in `nix/ext/` by category: |
| 81 | +- **Security**: pgsodium, vault, pgaudit |
| 82 | +- **Analytics**: TimescaleDB, pg_stat_monitor |
| 83 | +- **API**: pg_graphql, PostgREST integration |
| 84 | +- **Vector Search**: pgvector |
| 85 | +- **Geospatial**: PostGIS, pgRouting |
| 86 | + |
| 87 | +### Migration System |
| 88 | +- Uses **dbmate** for schema migrations |
| 89 | +- Located in `migrations/` directory |
| 90 | +- Supports all PostgreSQL versions |
| 91 | +- Append-only migration pattern |
| 92 | + |
| 93 | +### Testing Framework |
| 94 | +- **pg_regress**: PostgreSQL regression testing |
| 95 | +- **pgTAP**: Database unit testing in `migrations/tests/extensions/` |
| 96 | +- **testinfra**: Infrastructure testing with Python pytest |
| 97 | +- **PostgreSQL 17 testing**: All extensions tested against PostgreSQL 17 ONLY |
| 98 | + |
| 99 | +## Extension Development |
| 100 | + |
| 101 | +### Adding New Extensions |
| 102 | +1. Create build stage in appropriate Dockerfile |
| 103 | +2. Add version build args |
| 104 | +3. Use `checkinstall` for source-built extensions |
| 105 | +4. Copy package to extensions stage |
| 106 | +5. Add pgTAP test in `migrations/tests/extensions/` |
| 107 | + |
| 108 | +### Extension Testing |
| 109 | +Create test file in `migrations/tests/extensions/`: |
| 110 | +```sql |
| 111 | +BEGIN; |
| 112 | +create extension if not exists your_extension with schema "extensions"; |
| 113 | +ROLLBACK; |
| 114 | +``` |
| 115 | + |
| 116 | +## Migration Guidelines |
| 117 | +- **Never edit existing migrations** - always create new ones |
| 118 | +- **Idempotent**: All migrations must be rerunnable |
| 119 | +- **Role-based**: Use appropriate database roles for different migration phases |
| 120 | +- **PostgreSQL 17 specific**: Target PostgreSQL 17 ONLY |
| 121 | + |
| 122 | +## Security Model |
| 123 | +- **supabase_admin**: Superuser role for administration |
| 124 | +- **authenticator**: Connection pooling role |
| 125 | +- **Row Level Security**: Built-in RLS policies |
| 126 | +- **Predefined roles**: For API access patterns |
| 127 | + |
| 128 | +## Key Configuration |
| 129 | +- Default PostgreSQL port: `5435` |
| 130 | +- Default host: `localhost` |
| 131 | +- Superuser: `supabase_admin` |
| 132 | +- WAL level: `logical` with 5 replication slots |
| 133 | +- Large Systems Extensions enabled for ARM images |
0 commit comments