Skip to content

Commit 3b920f9

Browse files
author
guilherme
committed
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
1 parent 3960624 commit 3b920f9

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ DEL_INSTANCE=false
2929

3030
# Provider: postgresql | mysql
3131
DATABASE_PROVIDER=postgresql
32-
DATABASE_CONNECTION_URI='postgresql://user:pass@postgres:5432/evolution?schema=public'
32+
DATABASE_CONNECTION_URI='postgresql://user:pass@postgres:6543/evolution?pgbouncer=true&schema=evolution_api'
33+
DATABASE_DIRECT_CONNECTION_URI=postgresql://user:pass@postgres:5432/evolution?schema=evolution_api
3334
# Client name for the database connection
3435
# It is used to separate an API installation from another that uses the same database.
3536
DATABASE_CONNECTION_CLIENT_NAME=evolution_exchange

docker-compose.yaml

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
services:
2+
23
api:
34
container_name: evolution_api
45
image: evoapicloud/evolution-api:latest
@@ -34,19 +35,41 @@ services:
3435
image: postgres:15
3536
networks:
3637
- evolution-net
37-
command: ["postgres", "-c", "max_connections=1000", "-c", "listen_addresses=*"]
38+
command: [
39+
"postgres",
40+
"-c", "max_connections=200",
41+
"-c", "listen_addresses=*",
42+
"-c", "shared_buffers=256MB",
43+
"-c", "effective_cache_size=1GB",
44+
"-c", "work_mem=4MB"
45+
]
3846
restart: always
3947
ports:
4048
- 5432:5432
4149
environment:
4250
- POSTGRES_USER=user
4351
- POSTGRES_PASSWORD=pass
44-
- POSTGRES_DB=evolution
52+
- POSTGRES_DB=evolution_db
4553
- POSTGRES_HOST_AUTH_METHOD=trust
4654
volumes:
47-
- postgres_data:/var/lib/postgresql/data
48-
expose:
49-
- 5432
55+
- postgres_data:/var/lib/postgresql/data
56+
57+
pgbouncer:
58+
image: edoburu/pgbouncer:latest
59+
environment:
60+
DB_HOST: postgres
61+
DB_USER: user
62+
DB_PASSWORD: pass
63+
POOL_MODE: transaction
64+
AUTH_TYPE: trust
65+
MAX_CLIENT_CONN: 1000
66+
DEFAULT_POOL_SIZE: 25
67+
depends_on:
68+
- postgres
69+
ports:
70+
- "6543:5432"
71+
networks:
72+
- evolution-net
5073

5174
volumes:
5275
evolution_instances:

prisma/postgresql-schema.prisma

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ generator client {
99
}
1010

1111
datasource db {
12-
provider = "postgresql"
13-
url = env("DATABASE_CONNECTION_URI")
12+
provider = "postgresql"
13+
url = env("DATABASE_CONNECTION_URI")
14+
directUrl = env("DATABASE_DIRECT_CONNECTION_URI")
1415
}
1516

1617
enum InstanceConnectionStatus {
@@ -376,8 +377,8 @@ model TypebotSetting {
376377
debounceTime Int? @db.Integer
377378
typebotIdFallback String? @db.VarChar(100)
378379
ignoreJids Json?
379-
splitMessages Boolean? @default(false) @db.Boolean
380-
timePerChar Int? @default(50) @db.Integer
380+
splitMessages Boolean? @default(false) @db.Boolean
381+
timePerChar Int? @default(50) @db.Integer
381382
createdAt DateTime? @default(now()) @db.Timestamp
382383
updatedAt DateTime @updatedAt @db.Timestamp
383384
Fallback Typebot? @relation(fields: [typebotIdFallback], references: [id])
@@ -748,4 +749,4 @@ model EvoaiSetting {
748749
evoaiIdFallback String? @db.VarChar(100)
749750
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
750751
instanceId String @unique
751-
}
752+
}

0 commit comments

Comments
 (0)