PostgreSQL/PgBouncer: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| (13 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
{| | {|class='wikitable' style='width:100%;margin:-11px 0 6px 0' | ||
|valign= | |valign='top' style='width:50%'| | ||
<syntaxhighlight lang= | <syntaxhighlight style='margin:3px 0' lang='bash'> | ||
cat <<-'EXE'|sudo bash | cat <<-'EXE'|sudo bash | ||
apt-get update;echo | apt-get update;echo | ||
| Line 9: | Line 9: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|valign= | |valign='top' style='width:50%'| | ||
<syntaxhighlight lang= | <syntaxhighlight style='margin:3px 0' lang='bash'> | ||
systemctl status pgbouncer.service | systemctl status pgbouncer.service | ||
| Line 17: | Line 17: | ||
sudo systemctl restart pgbouncer.service | sudo systemctl restart pgbouncer.service | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|- | |||
|valign= | |valign='top'| | ||
<syntaxhighlight lang= | <syntaxhighlight style='margin:3px 0' lang='bash'> | ||
ls -lah /etc/pgbouncer/ | ls -lah /etc/pgbouncer/ | ||
| Line 26: | Line 26: | ||
sudo cat /etc/pgbouncer/pgbouncer.ini|less | sudo cat /etc/pgbouncer/pgbouncer.ini|less | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|valign='top'| | |||
|} | |} | ||
==Configure== | ==Configure== | ||
<syntaxhighlight lang= | {|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | ||
!scope='col' style='text-align:left' colspan='2'| | |||
Configure | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight style='margin:3px 0' lang='bash'> | |||
sudo rsync -avz /etc/pgbouncer/pgbouncer.ini /etc/pgbouncer/pgbouncer.ini.bkp | sudo rsync -avz /etc/pgbouncer/pgbouncer.ini /etc/pgbouncer/pgbouncer.ini.bkp | ||
sudo rsync -avz /etc/pgbouncer/userlist.txt /etc/pgbouncer/userlist.txt.bkp | sudo rsync -avz /etc/pgbouncer/userlist.txt /etc/pgbouncer/userlist.txt.bkp | ||
| Line 37: | Line 44: | ||
sudo cat /etc/pgbouncer/pgbouncer.ini | sudo cat /etc/pgbouncer/pgbouncer.ini | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|valign='top' style='width:50%'| | |||
|} | |||
===Configure » Pool » Database=== | ===Configure » Pool » Database=== | ||
--- | {|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | ||
<syntaxhighlight lang= | !scope='col' style='text-align:left'| | ||
Configure » Pool » Database | |||
|- | |||
|valign='top'| | |||
<syntaxhighlight style='margin:3px 0' lang='bash'> | |||
# find pgbouncer db pool | # find pgbouncer db pool | ||
PG_DB_POOL_FIND=$(cat <<QRY | PG_DB_POOL_FIND=$(cat <<QRY | ||
| Line 64: | Line 78: | ||
sudo sed -z "s|${PG_DB_POOL_FIND}|$(echo "${PG_DB_POOL_FILL}")|" -i /etc/pgbouncer/pgbouncer.ini | sudo sed -z "s|${PG_DB_POOL_FIND}|$(echo "${PG_DB_POOL_FILL}")|" -i /etc/pgbouncer/pgbouncer.ini | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |||
===Configure » Pool » User=== | ===Configure » Pool » User=== | ||
--- | {|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | ||
<syntaxhighlight lang= | !scope='col' style='text-align:left'| | ||
Configure » Pool » Database | |||
|- | |||
|valign='top'| | |||
<syntaxhighlight style='margin:3px 0' lang='bash'> | |||
# find pgbouncer user pool | # find pgbouncer user pool | ||
PG_USER_POOL_FIND=$(cat <<QRY | PG_USER_POOL_FIND=$(cat <<QRY | ||
| Line 88: | Line 107: | ||
sudo sed -z "s|auth_type = trust|auth_type = md5|" -i /etc/pgbouncer/pgbouncer.ini | sudo sed -z "s|auth_type = trust|auth_type = md5|" -i /etc/pgbouncer/pgbouncer.ini | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |||
===Configure » User » Secret=== | ===Configure » User » Secret=== | ||
--- | {|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | ||
<syntaxhighlight lang= | !scope='col' style='text-align:left' colspan='2'| | ||
Configure | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight style='margin:3px 0' lang='bash'> | |||
cat << USR | sudo tee -a /etc/pgbouncer/userlist.txt >/dev/null | cat << USR | sudo tee -a /etc/pgbouncer/userlist.txt >/dev/null | ||
"chorke" "md5$(printf '%s%s' 'p@$$w0rd' 'chorke'|md5sum|awk '{print $1}')" | "chorke" "md5$(printf '%s%s' 'p@$$w0rd' 'chorke'|md5sum|awk '{print $1}')" | ||
| Line 97: | Line 121: | ||
USR | USR | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|valign='top' style='width:50%'| | |||
|valign= | <syntaxhighlight style='margin:3px 0' lang='bash'> | ||
<syntaxhighlight lang= | |||
systemctl status pgbouncer.service | systemctl status pgbouncer.service | ||
sudo systemctl reload pgbouncer.service | sudo systemctl reload pgbouncer.service | ||
sudo systemctl restart pgbouncer.service | sudo systemctl restart pgbouncer.service | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|- | |||
|valign= | |valign='top'| | ||
<syntaxhighlight lang= | <syntaxhighlight style='margin:3px 0' lang='bash'> | ||
psql -hlocalhost -p6432 -Uchorke -dacademia_users_staging | psql -hlocalhost -p6432 -Uchorke -dacademia_users_staging | ||
psql -hlocalhost -p6432 -Uchorke -dacademia_audit_staging | psql -hlocalhost -p6432 -Uchorke -dacademia_audit_staging | ||
| Line 113: | Line 137: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|valign=" | |valign='top'| | ||
|} | |||
==PgBouncer.ini== | |||
{|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | |||
!scope='col' style='text-align:left' colspan='2'| | |||
PgBouncer.ini | |||
|- | |||
|valign='top' colspan='2'| | |||
<syntaxhighlight style='margin:3px 0' lang="ini"> | |||
[databases] | |||
academia_flair_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1' | |||
academia_audit_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1' | |||
academia_qoute_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1' | |||
academia_users_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1' | |||
[users] | |||
chorke = pool_mode=transaction max_user_connections=20 | |||
[pgbouncer] | |||
auth_type = md5 | |||
auth_file = /etc/pgbouncer/userlist.txt | |||
pool_mode = session | |||
ignore_startup_parameters = extra_float_digits | |||
min_pool_size = 5 | |||
reserve_pool_size = 5 | |||
reserve_pool_timeout = 5 | |||
max_db_connections = 10 | |||
max_user_connections = 10 | |||
</syntaxhighlight> | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight style='margin:3px 0' lang='bash'> | |||
sudo su | |||
su postgres | |||
pgbouncer -R /etc/pgbouncer/pgbouncer.ini | |||
</syntaxhighlight> | |||
|valign='top' style='width:50%'| | |||
|} | |} | ||
==Monitoring== | ==Monitoring== | ||
<syntaxhighlight lang="sql"> | {|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | ||
!scope='col' style='text-align:left' colspan='2'| | |||
Monitoring | |||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight style='margin:3px 0' lang="sql"> | |||
SHOW max_connections; | SHOW max_connections; | ||
RESET max_connections; | RESET max_connections; | ||
| Line 127: | Line 195: | ||
SELECT state, count(*) FROM pg_stat_activity WHERE pid <> pg_backend_pid() GROUP BY 1 ORDER BY 1; | SELECT state, count(*) FROM pg_stat_activity WHERE pid <> pg_backend_pid() GROUP BY 1 ORDER BY 1; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
---- | |valign='top' style='width:50%'| | ||
<syntaxhighlight lang="sql"> | |- | ||
|valign='top'| | |||
<syntaxhighlight style='margin:3px 0' lang="sql"> | |||
-- pg_lsclusters | |||
SHOW data_directory; | |||
SELECT setting FROM pg_settings WHERE name = 'data_directory'; | |||
SELECT name, setting FROM pg_settings WHERE setting LIKE '/%'; | |||
</syntaxhighlight> | |||
|valign='top'| | |||
|- | |||
|valign='top'| | |||
<syntaxhighlight style='margin:3px 0' lang="sql"> | |||
WITH conn_limit AS ( | WITH conn_limit AS ( | ||
SELECT setting::int "max_conn_size" | SELECT setting::int "max_conn_size" | ||
| Line 140: | Line 219: | ||
FROM pg_stat_activity; | FROM pg_stat_activity; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|valign='top'| | |||
|} | |||
==Playground== | ==Playground== | ||
{| | {|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | ||
|valign='top'| | !scope='col' style='text-align:left' colspan='2'| | ||
<syntaxhighlight lang= | Playground | ||
|- | |||
|valign='top' style='width:50%'| | |||
<syntaxhighlight style='margin:3px 0' lang='bash'> | |||
ls -lah /etc/pgbouncer/ | ls -lah /etc/pgbouncer/ | ||
ls -lah /var/log/postgresql/ | ls -lah /var/log/postgresql/ | ||
| Line 150: | Line 234: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|valign='top'| | |valign='top' style='width:50%'| | ||
<syntaxhighlight lang="ini"> | <syntaxhighlight style='margin:3px 0' lang="ini"> | ||
[pgbouncer] | [pgbouncer] | ||
; ERROR: unsupported startup parameter | ; ERROR: unsupported startup parameter | ||
ignore_startup_parameters = extra_float_digits | ignore_startup_parameters = extra_float_digits | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |||
| | ==References== | ||
{|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' | |||
!scope='col' style='text-align:left' colspan='3'| | |||
References | |||
|- | |- | ||
|valign='top' | |valign='top' style='width:33%'| | ||
* [https://stackoverflow.com/questions/43903485/ PostgreSQL » PgBouncer » Horizontal Scaling Advantage] | * [https://stackoverflow.com/questions/43903485/ PostgreSQL » PgBouncer » Horizontal Scaling Advantage] | ||
* [https://stackoverflow.com/questions/30778015/ PostgreSQL » Increase the max connections] | * [https://stackoverflow.com/questions/30778015/ PostgreSQL » Increase the max connections] | ||
| Line 185: | Line 259: | ||
* [[PostgreSQL]] | * [[PostgreSQL]] | ||
|valign= | |valign='top' style='width:34%'| | ||
* [https://stackoverflow.com/questions/36495062/ PostgreSQL » PgBouncer » <code>extra_float_digits</code>] | * [https://stackoverflow.com/questions/36495062/ PostgreSQL » PgBouncer » <code>extra_float_digits</code>] | ||
* [https://github.com/pgbouncer/pgbouncer/issues/174 PostgreSQL » PgBouncer » <code>reserve_pool_size</code>] | |||
* [https://dba.stackexchange.com/questions/1350/ PostgreSQL » Find data directory] | * [https://dba.stackexchange.com/questions/1350/ PostgreSQL » Find data directory] | ||
|valign= | |valign='top' style='width:33%'| | ||
|- | |- | ||
|valign='top'| | |||
|valign= | |||
* [[Academia JavaEE Workspace in Ubuntu]] | * [[Academia JavaEE Workspace in Ubuntu]] | ||
* [[IntelliJ IDEA]] | * [[IntelliJ IDEA]] | ||
| Line 207: | Line 279: | ||
* [[K8s]] | * [[K8s]] | ||
|valign= | |valign='top'| | ||
* [[K8s/Swiss Knife|K8s » Swiss Knife]] | * [[K8s/Swiss Knife|K8s » Swiss Knife]] | ||
* [[K8s/Ingress|K8s » Ingress]] | * [[K8s/Ingress|K8s » Ingress]] | ||
| Line 213: | Line 285: | ||
* [[K8s/Run|K8s » Run]] | * [[K8s/Run|K8s » Run]] | ||
|valign= | |valign='top'| | ||
|} | |} | ||
Latest revision as of 01:11, 16 January 2026
cat <<-'EXE'|sudo bash
apt-get update;echo
apt list -a --upgradable
apt-get install -y pgbouncer
EXE
|
systemctl status pgbouncer.service
sudo systemctl stop pgbouncer.service
sudo systemctl start pgbouncer.service
sudo systemctl restart pgbouncer.service
|
ls -lah /etc/pgbouncer/
sudo cat /etc/pgbouncer/userlist.txt
sudo cat /etc/pgbouncer/pgbouncer.ini
sudo cat /etc/pgbouncer/pgbouncer.ini|less
|
Configure
|
Configure | |
|---|---|
sudo rsync -avz /etc/pgbouncer/pgbouncer.ini /etc/pgbouncer/pgbouncer.ini.bkp
sudo rsync -avz /etc/pgbouncer/userlist.txt /etc/pgbouncer/userlist.txt.bkp
# restore default configuration
sudo rsync -avz /etc/pgbouncer/pgbouncer.ini.bkp /etc/pgbouncer/pgbouncer.ini
sudo cat /etc/pgbouncer/pgbouncer.ini
|
|
Configure » Pool » Database
|
Configure » Pool » Database |
|---|
# find pgbouncer db pool
PG_DB_POOL_FIND=$(cat <<QRY
\[databases\]\n\
\n\
;; foodb over Unix socket\n\
;foodb =
QRY
)
# fill pgbouncer db pool
PG_DB_POOL_FILL=$(cat <<UPD
[databases]\n\
\n\
;; future db pool here\n\
academia_flair_staging = host=10.110.110.155 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'\n\
academia_audit_staging = host=10.110.110.155 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'\n\
academia_qoute_staging = host=10.110.110.155 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'\n\
academia_users_staging = host=10.110.110.155 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
UPD
)
sudo sed -z "s|${PG_DB_POOL_FIND}|$(echo "${PG_DB_POOL_FILL}")|" -i /etc/pgbouncer/pgbouncer.ini
|
Configure » Pool » User
|
Configure » Pool » Database |
|---|
# find pgbouncer user pool
PG_USER_POOL_FIND=$(cat <<QRY
\[users\]\n\
\n\
;user1 = pool_mode=transaction max_user_connections=10
QRY
)
# fill pgbouncer user pool
PG_USER_POOL_FILL=$(cat <<UPD
[users]\n\
\n\
;; future user pool here\n\
chorke = pool_mode=transaction max_user_connections=10
UPD
)
sudo sed -z "s|${PG_USER_POOL_FIND}|$(echo "${PG_USER_POOL_FILL}")|" -i /etc/pgbouncer/pgbouncer.ini
sudo sed -z "s|auth_type = trust|auth_type = md5|" -i /etc/pgbouncer/pgbouncer.ini
|
Configure » User » Secret
|
Configure | |
|---|---|
cat << USR | sudo tee -a /etc/pgbouncer/userlist.txt >/dev/null
"chorke" "md5$(printf '%s%s' 'p@$$w0rd' 'chorke'|md5sum|awk '{print $1}')"
"shahed" "md5$(printf '%s%s' 'sadaqah!' 'shahed'|md5sum|awk '{print $1}')"
USR
|
systemctl status pgbouncer.service
sudo systemctl reload pgbouncer.service
sudo systemctl restart pgbouncer.service
|
psql -hlocalhost -p6432 -Uchorke -dacademia_users_staging
psql -hlocalhost -p6432 -Uchorke -dacademia_audit_staging
psql -hlocalhost -p6432 -Uchorke -dacademia_flair_staging
|
|
PgBouncer.ini
|
PgBouncer.ini | |
|---|---|
[databases]
academia_flair_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
academia_audit_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
academia_qoute_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
academia_users_staging = host=127.0.0.1 port=5432 user=chorke password='sadaqah!' client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
[users]
chorke = pool_mode=transaction max_user_connections=20
[pgbouncer]
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
pool_mode = session
ignore_startup_parameters = extra_float_digits
min_pool_size = 5
reserve_pool_size = 5
reserve_pool_timeout = 5
max_db_connections = 10
max_user_connections = 10
| |
sudo su
su postgres
pgbouncer -R /etc/pgbouncer/pgbouncer.ini
|
|
Monitoring
|
Monitoring | |
|---|---|
SHOW max_connections;
RESET max_connections;
ALTER SYSTEM SET max_connections = 200;
SELECT current_setting('max_connections');
SELECT sum(numbackends) FROM pg_stat_database;
SELECT * FROM pg_settings WHERE name = 'max_connections';
SELECT state, count(*) FROM pg_stat_activity WHERE pid <> pg_backend_pid() GROUP BY 1 ORDER BY 1;
|
|
-- pg_lsclusters
SHOW data_directory;
SELECT setting FROM pg_settings WHERE name = 'data_directory';
SELECT name, setting FROM pg_settings WHERE setting LIKE '/%';
|
|
WITH conn_limit AS (
SELECT setting::int "max_conn_size"
FROM pg_settings WHERE name=$$max_connections$$
),
conn_super AS (
SELECT setting::int "max_conn_admin"
FROM pg_settings WHERE name=$$superuser_reserved_connections$$
)
SELECT count(*) "max_conn_used", (SELECT * FROM conn_super), (SELECT * FROM conn_limit)
FROM pg_stat_activity;
|
|
Playground
|
Playground | |
|---|---|
ls -lah /etc/pgbouncer/
ls -lah /var/log/postgresql/
ls -lah /var/run/postgresql/
|
[pgbouncer]
; ERROR: unsupported startup parameter
ignore_startup_parameters = extra_float_digits
|