Berikut cara install **PgBouncer** di Ubuntu (termasuk Ubuntu Server 24.04/26.04). --- # 1. Install Update package: ```bash sudo apt update ``` Install PgBouncer: ```bash sudo apt install pgbouncer ``` Cek versi: ```bash pgbouncer -V ``` Contoh output: ```text PgBouncer 1.22.x ``` --- # 2. File konfigurasi Biasanya berada di: ```text /etc/pgbouncer/ ``` Lihat isinya: ```bash ls /etc/pgbouncer ``` Biasanya terdapat: ```text pgbouncer.ini userlist.txt ``` --- # 3. Konfigurasi pgbouncer.ini Edit: ```bash sudo nano /etc/pgbouncer/pgbouncer.ini ``` Contoh konfigurasi: ```ini [databases] mydb = host=127.0.0.1 port=5432 dbname=mydb [pgbouncer] listen_addr = 0.0.0.0 listen_port = 6432 auth_type = md5 auth_file = /etc/pgbouncer/userlist.txt pool_mode = transaction max_client_conn = 1000 default_pool_size = 50 min_pool_size = 10 reserve_pool_size = 10 ignore_startup_parameters = extra_float_digits server_reset_query = DISCARD ALL log_connections = 1 log_disconnections = 1 ``` --- # 4. Membuat password MD5 PgBouncer dengan `auth_type = md5` membutuhkan password dalam format khusus. Misalnya: User: ```text postgres ``` Password: ```text admin123 ``` Hitung hash: ```bash echo -n "admin123postgres" | md5sum ``` Hasil misalnya: ```text 6b0f9e0d9a5f8f4c.... ``` Masukkan ke `userlist.txt`: ```bash sudo nano /etc/pgbouncer/userlist.txt ``` Isi: ```text "postgres" "md56b0f9e0d9a5f8f4c..." ``` Perhatikan formatnya: ```text md5 + md5(password + username) ``` --- # 5. Restart service ```bash sudo systemctl restart pgbouncer ``` Cek status: ```bash sudo systemctl status pgbouncer ``` Enable saat boot: ```bash sudo systemctl enable pgbouncer ``` --- # 6. Cek port ```bash sudo ss -ltnp | grep 6432 ``` Harus muncul: ```text LISTEN 0 128 0.0.0.0:6432 ``` --- # 7. Test koneksi Gunakan `psql`: ```bash psql -h localhost -p 6432 -U postgres mydb ``` Kalau berhasil berarti PgBouncer sudah aktif. --- # 8. Ubah aplikasi Go Sebelumnya: ```text postgres://postgres:password@localhost:5432/mydb ``` Menjadi: ```text postgres://postgres:password@localhost:6432/mydb ``` Hanya port yang berubah. --- # 9. Monitoring PgBouncer Masuk ke database virtual `pgbouncer`: ```bash psql -h localhost -p 6432 -U postgres pgbouncer ``` Lihat pool: ```sql SHOW POOLS; ``` Lihat client: ```sql SHOW CLIENTS; ``` Lihat server: ```sql SHOW SERVERS; ``` Lihat statistik: ```sql SHOW STATS; ``` --- # 10. Setting PostgreSQL Jika memakai PgBouncer, biasanya `max_connections` di PostgreSQL tidak perlu terlalu besar. Contoh: ```conf max_connections = 100 ``` Sedangkan PgBouncer: ```ini default_pool_size = 50 max_client_conn = 1000 ``` Artinya: ```text 1000 Client │ ▼ PgBouncer │ 50 koneksi aktif │ PostgreSQL ``` --- ## Rekomendasi untuk aplikasi Go Karena Anda sedang mengembangkan backend Go dengan `database/sql`, konfigurasi berikut umumnya cocok: **PostgreSQL** ```conf max_connections = 100 ``` **PgBouncer** ```ini pool_mode = transaction default_pool_size = 30 max_client_conn = 1000 ``` **Go (`database/sql`)** ```go db.SetMaxOpenConns(10) db.SetMaxIdleConns(5) db.SetConnMaxLifetime(30 * time.Minute) db.SetConnMaxIdleTime(10 * time.Minute) ``` ``` dsn := "postgres://postgres:admin123@192.168.1.10:6432/koperasi?sslmode=disable" ``` Konekkan keport PgBouncer Konfigurasi ini biasanya mampu menangani beban yang jauh lebih tinggi dibandingkan aplikasi yang terhubung langsung ke PostgreSQL, sambil menjaga jumlah koneksi database tetap efisien. video : https://www.youtube.com/watch?v=TyB5cW8vOQU