|
| 1 | +## Overview |
| 2 | + |
| 3 | +We migrated to Google Cloud SQL PostgreSQL due to memory and storage limitations on our previous database. The setup uses a cost-effective shared-core instance suitable for development environments. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Google Cloud SDK (`gcloud`) installed and authenticated |
| 8 | +- Access to the `nava-labs` GCP project |
| 9 | +- Appropriate IAM permissions for Cloud SQL |
| 10 | + |
| 11 | +## Database Configuration |
| 12 | + |
| 13 | +### Instance Details |
| 14 | +- **Instance Name:** `app-dev` |
| 15 | +- **Database Engine:** PostgreSQL 15 |
| 16 | +- **Tier:** `db-g1-small` (1.7 GB RAM, shared-core) |
| 17 | +- **Region:** `us-central1` |
| 18 | +- **Storage:** 100GB SSD with automatic backups enabled |
| 19 | +- **IP Address:** `your_ip_address` |
| 20 | + |
| 21 | +### Database & User |
| 22 | +- **Database Name:** `app_db` |
| 23 | +- **Username:** `app_user` |
| 24 | +- **Password:** `your_password` |
| 25 | + |
| 26 | +## Setup Instructions |
| 27 | + |
| 28 | +### 1. Verify GCP Project Configuration |
| 29 | + |
| 30 | +```bash |
| 31 | +# Check current project |
| 32 | +gcloud config get-value project |
| 33 | +# Should return: nava-labs |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Create PostgreSQL Instance |
| 37 | + |
| 38 | +```bash |
| 39 | +gcloud sql instances create app-dev \ |
| 40 | + --database-version=POSTGRES_15 \ |
| 41 | + --tier=db-g1-small \ |
| 42 | + --region=us-central1 \ |
| 43 | + --storage-type=SSD \ |
| 44 | + --storage-size=100GB \ |
| 45 | + --backup |
| 46 | +``` |
| 47 | + |
| 48 | +**Note:** PostgreSQL on Cloud SQL only supports custom tiers or shared-core tiers (not the standard predefined tiers like `db-n1-standard-*`). |
| 49 | + |
| 50 | +### 3. Create Database |
| 51 | + |
| 52 | +```bash |
| 53 | +gcloud sql databases create app_db --instance=app-dev |
| 54 | +``` |
| 55 | + |
| 56 | +### 4. Create Database User |
| 57 | + |
| 58 | +```bash |
| 59 | +gcloud sql users create app_user \ |
| 60 | + --instance=app-dev \ |
| 61 | + --password=your_password |
| 62 | +``` |
| 63 | + |
| 64 | +### 5. Get Connection Information |
| 65 | + |
| 66 | +```bash |
| 67 | +# Get the instance IP address |
| 68 | +gcloud sql instances describe app-dev \ |
| 69 | + --format="value(ipAddresses[0].ipAddress)" |
| 70 | +``` |
| 71 | + |
| 72 | +### 6. Authorize Your IP Address |
| 73 | + |
| 74 | +Before you can connect to the database, you need to add your current IP address to the authorized networks: |
| 75 | + |
| 76 | +```bash |
| 77 | +# Get your current public IP address |
| 78 | +curl -s https://ipinfo.io/ip |
| 79 | + |
| 80 | +# Add your IP to the authorized networks (replace YOUR_IP with the actual IP) |
| 81 | +gcloud sql instances patch app-dev --authorized-networks=YOUR_IP |
| 82 | +``` |
| 83 | + |
| 84 | +**Important:** When adding a new IP address, make sure to include any previously authorized IPs, otherwise they will be overwritten. |
| 85 | + |
| 86 | +### 7. Update Environment Configuration |
| 87 | + |
| 88 | +Update your `.env` file with the new database connection string: |
| 89 | + |
| 90 | +```env |
| 91 | +# New GCP Cloud SQL PostgreSQL |
| 92 | +DATABASE_URL="postgresql://app_user:your_password@your_ip_address/app_db?sslmode=require" |
| 93 | +``` |
| 94 | + |
| 95 | +## Connection String Format |
| 96 | + |
| 97 | +``` |
| 98 | +postgresql://[username]:[password]@[host]:[port]/[database]?[parameters] |
| 99 | +``` |
| 100 | + |
| 101 | +For our setup: |
| 102 | +- **Username:** `app_user` |
| 103 | +- **Password:** `your_password` |
| 104 | +- **Host:** `your_ip_address` |
| 105 | +- **Port:** `5432` (default, omitted) |
| 106 | +- **Database:** `app_db` |
| 107 | +- **SSL Mode:** `require` (mandatory for Cloud SQL) |
| 108 | + |
| 109 | +## Database Migration |
| 110 | + |
| 111 | +After setting up the new database and authorizing your IP, run Prisma migrations to set up the schema: |
| 112 | + |
| 113 | +```bash |
| 114 | +# Deploy existing migrations to the new database |
| 115 | +npx prisma migrate deploy |
| 116 | + |
| 117 | +# Generate the Prisma client |
| 118 | +npx prisma generate |
| 119 | + |
| 120 | +# Verify the schema is synchronized (optional) |
| 121 | +npx prisma db push --accept-data-loss |
| 122 | +``` |
| 123 | + |
| 124 | +**Note:** If you get a connection error, make sure you've completed step 6 (IP authorization) above. |
| 125 | + |
| 126 | +## Environment Naming Convention |
| 127 | + |
| 128 | +Following our multi-environment strategy: |
| 129 | +- **Development:** `app-dev` (current setup) |
| 130 | +- **Staging:** `app-staging` (future) |
| 131 | +- **Production:** `app-prod` (future) |
| 132 | + |
| 133 | +## Cost Considerations |
| 134 | + |
| 135 | +- **Tier:** `db-g1-small` is cost-effective for development |
| 136 | +- **Storage:** 100GB SSD provides good performance |
| 137 | +- **Backups:** Enabled for data safety |
| 138 | +- **Region:** `us-central1` offers good pricing |
| 139 | + |
| 140 | +## Troubleshooting |
| 141 | + |
| 142 | +### Common Issues |
| 143 | + |
| 144 | +1. **"Only custom or shared-core instance Billing Tier type allowed for PostgreSQL"** |
| 145 | + - Use `db-g1-small` or `db-custom-X-Y` tiers, not `db-n1-standard-*` |
| 146 | + |
| 147 | +2. **"Can't reach database server" or connection timeouts** |
| 148 | + - Ensure your IP is authorized: `gcloud sql instances patch app-dev --authorized-networks=YOUR_IP` |
| 149 | + - Verify SSL mode is set to `require` |
| 150 | + - Check that the instance is running: `gcloud sql instances describe app-dev` |
| 151 | + |
| 152 | +3. **Prisma migration fails** |
| 153 | + - Make sure IP authorization is complete before running migrations |
| 154 | + - Verify the DATABASE_URL in your `.env` file is correct |
| 155 | + |
| 156 | +### Useful Commands |
| 157 | + |
| 158 | +```bash |
| 159 | +# List all SQL instances |
| 160 | +gcloud sql instances list |
| 161 | + |
| 162 | +# Get instance details |
| 163 | +gcloud sql instances describe app-dev |
| 164 | + |
| 165 | +# List databases in instance |
| 166 | +gcloud sql databases list --instance=app-dev |
| 167 | + |
| 168 | +# List users in instance |
| 169 | +gcloud sql users list --instance=app-dev |
| 170 | + |
| 171 | +# Connect via gcloud (for testing) |
| 172 | +gcloud sql connect app-dev --user=app_user --database=app_db |
| 173 | +``` |
| 174 | + |
| 175 | +## Security Notes |
| 176 | + |
| 177 | +- Database password is stored in `.env` file (ensure it's in `.gitignore`) |
| 178 | +- SSL is required for all connections |
| 179 | +- Plan to use Cloud SQL Proxy for better security in production |
| 180 | +- IP whitelisting should be configured for production environments |
0 commit comments