|
| 1 | +# Set up PgBouncer for Connection Pooling with OCI Database with PostgreSQL |
| 2 | + |
| 3 | +Often, clients would like to add connection pooling to their OCI Database with PostgreSQL. |
| 4 | +The following steps are an example to set up connection pooling, using [PgBouncer](https://www.pgbouncer.org/). |
| 5 | + |
| 6 | +The set up is similar to below simplified architecture. Connections are passed through a Network Load Balancer, with the pg_bouncer instances as backend set. |
| 7 | +Following, the pg_bouncer - as the connection pooler - connect to multiple PostgreSQL databases or the invidual nodes, to differentiate between read/write and read-only. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +# 1. Set up OCI Database with PostgreSQL |
| 12 | +Follow [these steps to set up one or more OCI Database with PostgreSQL deployments](https://docs.oracle.com/en-us/iaas/Content/postgresql/getting-started.htm#database) |
| 13 | + |
| 14 | +- After creating the databases, review the max_connections you have in the database. |
| 15 | +- Make sure to open ports (5432) in your public/private subnet |
| 16 | + |
| 17 | +# 2. Set up Network Load Balancer |
| 18 | +Follow [these steps to set up a NLB on OCI](https://docs.oracle.com/en-us/iaas/Content/NetworkLoadBalancer/NetworkLoadBalancers/network-load-balancer-management.htm). |
| 19 | +For the back end set, you will need to add the OCI Compute instances running pg_bouncer. |
| 20 | + |
| 21 | +# 3. Set up multiple OCI Compute instances to run pg_bouncer |
| 22 | + |
| 23 | +The below example uses Canonical Ubuntu 24.04 and assumes your PostgreSQL database is version 14. |
| 24 | + |
| 25 | +- Install pg_bouncer on the compute. |
| 26 | + ``` |
| 27 | + sudo apt-get install pgbouncer |
| 28 | + ``` |
| 29 | + |
| 30 | +- Configure the user list. These are the admin credentials. |
| 31 | + ``` |
| 32 | + sudo vim etc/pgbouncer/userlist.txt |
| 33 | + ``` |
| 34 | + |
| 35 | +- Add your username and password of your OCI Database with PostgreSQL instances. |
| 36 | + ``` |
| 37 | + "Example_user" "example_password" |
| 38 | + ``` |
| 39 | + |
| 40 | +- Change the pg_bouncer config file. These determine the connection details to the database, the auth type, session type, etc. |
| 41 | + ``` |
| 42 | + sudo vim etc/pgbouncer/pgbouncer.ini |
| 43 | + ``` |
| 44 | + |
| 45 | + - Change the [database] section to the below. The hosts and dbname are examples. |
| 46 | + ``` |
| 47 | + [databases] |
| 48 | +
|
| 49 | + writer = host=[YOUR_PRIMARY_ENDPOINT] port=5432 dbname=postgres |
| 50 | + reader = host=[YOUR_READER_ENDPOINT] port=5432 dbname=postgres |
| 51 | + ``` |
| 52 | +
|
| 53 | + - Change the list_addr to * |
| 54 | + ``` |
| 55 | + listen_addr = * |
| 56 | + ``` |
| 57 | +
|
| 58 | + - Uncomment the below. Change the client connections and pool size to your needs. |
| 59 | + ``` |
| 60 | + pool_mode = session |
| 61 | + server_reset_query = DISCARD ALL |
| 62 | + max_client_conn = 100 |
| 63 | + default_pool_size = 20 |
| 64 | + ``` |
| 65 | +
|
| 66 | + Save and close the file. An example of config file. |
| 67 | + |
| 68 | +  |
| 69 | +
|
| 70 | +- Restart pg_bouncer and review status |
| 71 | + ``` |
| 72 | + sudo systemctl restart pgbouncer.service |
| 73 | + systemctl status pgbouncer.service |
| 74 | + ```` |
| 75 | +
|
| 76 | +  |
| 77 | +
|
| 78 | +# 4 Review PgBouncer connection |
| 79 | +
|
| 80 | +- Install PostgreSQL client if not done |
| 81 | + ``` |
| 82 | + sudo apt-get install postgresql postgresql-client |
| 83 | + ``` |
| 84 | +
|
| 85 | +- Connect using psql, through pg_bouncer. Add your password when prompted. |
| 86 | + ``` |
| 87 | + psql -h localhost -p 6432 -U pgadmin -d writer |
| 88 | + ``` |
| 89 | +
|
| 90 | + When working, you should see Writer =>. |
| 91 | + |
0 commit comments