feat(database): add pgbouncer support and optimize postgres config

- Add pgbouncer service to handle connection pooling
- Update database connection URIs to support direct and pooled connections
- Optimize postgres configuration with better memory settings
- Update prisma schema to support directUrl connection
This commit is contained in:
guilherme 2025-07-08 17:14:37 -03:00
parent 39606240da
commit 3b920f93c5
3 changed files with 36 additions and 11 deletions

View File

@ -29,7 +29,8 @@ DEL_INSTANCE=false
# Provider: postgresql | mysql # Provider: postgresql | mysql
DATABASE_PROVIDER=postgresql DATABASE_PROVIDER=postgresql
DATABASE_CONNECTION_URI='postgresql://user:pass@postgres:5432/evolution?schema=public' DATABASE_CONNECTION_URI='postgresql://user:pass@postgres:6543/evolution?pgbouncer=true&schema=evolution_api'
DATABASE_DIRECT_CONNECTION_URI=postgresql://user:pass@postgres:5432/evolution?schema=evolution_api
# Client name for the database connection # Client name for the database connection
# It is used to separate an API installation from another that uses the same database. # It is used to separate an API installation from another that uses the same database.
DATABASE_CONNECTION_CLIENT_NAME=evolution_exchange DATABASE_CONNECTION_CLIENT_NAME=evolution_exchange

View File

@ -1,4 +1,5 @@
services: services:
api: api:
container_name: evolution_api container_name: evolution_api
image: evoapicloud/evolution-api:latest image: evoapicloud/evolution-api:latest
@ -34,19 +35,41 @@ services:
image: postgres:15 image: postgres:15
networks: networks:
- evolution-net - evolution-net
command: ["postgres", "-c", "max_connections=1000", "-c", "listen_addresses=*"] command: [
"postgres",
"-c", "max_connections=200",
"-c", "listen_addresses=*",
"-c", "shared_buffers=256MB",
"-c", "effective_cache_size=1GB",
"-c", "work_mem=4MB"
]
restart: always restart: always
ports: ports:
- 5432:5432 - 5432:5432
environment: environment:
- POSTGRES_USER=user - POSTGRES_USER=user
- POSTGRES_PASSWORD=pass - POSTGRES_PASSWORD=pass
- POSTGRES_DB=evolution - POSTGRES_DB=evolution_db
- POSTGRES_HOST_AUTH_METHOD=trust - POSTGRES_HOST_AUTH_METHOD=trust
volumes: volumes:
- postgres_data:/var/lib/postgresql/data - postgres_data:/var/lib/postgresql/data
expose:
- 5432 pgbouncer:
image: edoburu/pgbouncer:latest
environment:
DB_HOST: postgres
DB_USER: user
DB_PASSWORD: pass
POOL_MODE: transaction
AUTH_TYPE: trust
MAX_CLIENT_CONN: 1000
DEFAULT_POOL_SIZE: 25
depends_on:
- postgres
ports:
- "6543:5432"
networks:
- evolution-net
volumes: volumes:
evolution_instances: evolution_instances:

View File

@ -9,8 +9,9 @@ generator client {
} }
datasource db { datasource db {
provider = "postgresql" provider = "postgresql"
url = env("DATABASE_CONNECTION_URI") url = env("DATABASE_CONNECTION_URI")
directUrl = env("DATABASE_DIRECT_CONNECTION_URI")
} }
enum InstanceConnectionStatus { enum InstanceConnectionStatus {
@ -376,8 +377,8 @@ model TypebotSetting {
debounceTime Int? @db.Integer debounceTime Int? @db.Integer
typebotIdFallback String? @db.VarChar(100) typebotIdFallback String? @db.VarChar(100)
ignoreJids Json? ignoreJids Json?
splitMessages Boolean? @default(false) @db.Boolean splitMessages Boolean? @default(false) @db.Boolean
timePerChar Int? @default(50) @db.Integer timePerChar Int? @default(50) @db.Integer
createdAt DateTime? @default(now()) @db.Timestamp createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp updatedAt DateTime @updatedAt @db.Timestamp
Fallback Typebot? @relation(fields: [typebotIdFallback], references: [id]) Fallback Typebot? @relation(fields: [typebotIdFallback], references: [id])
@ -748,4 +749,4 @@ model EvoaiSetting {
evoaiIdFallback String? @db.VarChar(100) evoaiIdFallback String? @db.VarChar(100)
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String @unique instanceId String @unique
} }