Home
%3CLINGO-SUB%20id%3D%22lingo-sub-730555%22%20slang%3D%22en-US%22%3ESteps%20to%20install%20and%20setup%20PgBouncer%20connection%20pooling%20proxy%20with%20Azure%20DB%20for%20PostgreSQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-730555%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20my%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fblog%2Fperformance-best-practices-for-using-azure-database-for-postgresql-connection-pooling%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eprevious%20blog%20post%3C%2FA%3E%20on%20Azure%20blogs%2C%20I%20explain%20why%20it%20is%20important%20to%20leverage%20connection%20pooling%20proxy%20with%20PostgreSQL%20and%20how%20using%20a%20connection%20pooling%20proxy%20like%20PgBouncer%20against%20Azure%20Database%20for%20PostgreSQL%20can%20provide%20a%20significant%20performance%20boost%2C%20assuming%20connection%20pooling%20is%20not%20used%20at%20the%20application%20layer.%20To%20give%20some%20estimates%20of%20the%20performance%20improvement%20when%20using%20PgBouncer%20for%20connection%20pooling%20with%20Azure%20Database%20for%20PostgreSQL%2C%20we%20ran%20a%20simple%20performance%20benchmark%20test%20with%20pgbench.%20pgbench%20provides%20a%20configuration%20setting%20to%20create%20new%20connection%20for%20every%20transaction%20so%20we%20leveraged%20that%20to%20measure%20the%20impact%20of%20connection%20latency%20on%20throughput%20of%20the%20application.%20The%20following%20are%20the%20observations%20with%20A%2FB%20testing%20comparing%20throughput%20with%20standard%20pgbench%20benchmark%20testing%20with%20and%20without%20PgBouncer.%20In%20the%20test%2C%20we%20ran%20pgbench%20with%20scale%20factor%20of%205%20against%20Azure%20Database%20for%20PostgreSQL%20running%20on%20general%20purpose%20tier%20with%202%20vCores%20(GP_Gen5_2).%20The%20only%20variable%20during%20the%20tests%20was%20PgBouncer.%20With%20PgBouncer%2C%20the%26nbsp%3B%3CSTRONG%3Ethroughput%20improved%204x%20times%20as%20shown%20below%20while%20connection%20latency%20was%20reduced%20by%2040%20percent%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20700px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F121737i363E08FEFF67D931%2Fimage-dimensions%2F700x310%3Fv%3D1.0%22%20width%3D%22700%22%20height%3D%22310%22%20alt%3D%22PerfImprovementwithPgBouncer.png%22%20title%3D%22PerfImprovementwithPgBouncer.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20blog%20post%2C%20we%20share%20the%20steps%20to%20install%20and%20setup%20PgBouncer%20connection%20pooling%20proxy%20between%20application%20and%20Azure%20DB%20for%20PostgreSQL%20to%20reap%20the%20performance%20and%20resiliency%20benefits%20it%20offers.%20In%20addition%2C%20we%20also%20provide%20you%20with%20a%20QuickStart%20template%20at%20the%20end%20of%20the%20blog%20post%20which%20can%20enable%20you%20to%20quickly%20deploy%20and%20setup%20a%20PgBouncer%20in%20a%20VM%20and%20get%20started.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPgBouncer%20connection%20proxy%20is%20setup%20between%20the%20application%20and%20database%20layer%20as%20shown%20in%20the%20image%20below.%20As%20Azure%20DB%20for%20PostgreSQL%20is%20a%20fully%20managed%20platform%20service%2C%20you%20won%E2%80%99t%20have%20access%20to%20install%20any%20external%20component%20on%20database%20server.%20In%20this%20case%2C%20if%20you%20are%20running%20your%20application%20on%20an%20Azure%20VM%2C%20you%20can%20setup%20PgBouncer%20on%20the%20same%20VM%20running%20the%20application.%20If%20the%20application%20runs%20on%20a%20managed%20service%20like%20Azure%20App%20Services%20or%20Azure%20Functions%2C%20you%20may%20have%20to%20provision%20a%20separate%20Ubuntu%20VM%20to%20run%20PgBouncer%20proxy%20service.%20However%2C%20if%20you%20are%20running%20your%20application%20on%20Azure%20Kubernetes%20Services%20(AKS)%2C%20you%20can%20leverage%20%3CA%20href%3D%22https%3A%2F%2Fhub.docker.com%2F_%2Fmicrosoft-azure-oss-db-tools-pgbouncer-sidecar%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPgBouncer%20sidecar%3C%2FA%3E%20to%20run%20it%20as%20a%20Kubernetes%20sidecar%20to%20your%20application.%20In%20this%20blog%20post%2C%20we%20will%20focus%20on%20steps%20to%20install%20and%20run%20PgBouncer%20on%20Ubuntu%20VM%20while%20steps%20to%20setup%20PgBouncer%20sidecar%20can%20be%20a%20topic%20for%20a%20follow-up%20blog%20post.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20427px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F121738iC727A324F10A610F%2Fimage-dimensions%2F427x277%3Fv%3D1.0%22%20width%3D%22427%22%20height%3D%22277%22%20alt%3D%22Connection_pooling.jpg%22%20title%3D%22Connection_pooling.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1790502461%22%20id%3D%22toc-hId-1790502461%22%3ESteps%20to%20setup%20PgBouncer%20on%20Ubuntu%20VM%3C%2FH2%3E%0A%3CP%3EAll%20the%20steps%20mentioned%20below%20uses%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Finstall-azure-cli%3Fview%3Dazure-cli-latest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20CLI%3C%2FA%3E%20to%20provision%20and%20deploy%20resources.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EProvision%20an%20Ubuntu%20VM.%20If%20your%20application%20is%20already%20running%20on%20Azure%20VM%20and%20you%20have%20a%20VM%20provisioned%2C%20you%20can%20skip%20this%20step%20and%20move%20to%20step%202.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%3Eaz%3C%2FSPAN%3E%20%3CSPAN%3Egroup%3C%2FSPAN%3E%20%3CSPAN%3Ecreate%3C%2FSPAN%3E%3CSPAN%3E%20--name%3C%2FSPAN%3E%20myResourceGroup%3CSPAN%3E%20--location%3C%2FSPAN%3E%20eastus%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20az%20vm%20create%20%5C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20--resource-group%20myResourceGroup%20%5C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20--name%20PgBouncerPoolVM%20%5C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20--image%20UbuntuLTS%20%5C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20--admin-username%20pgadminuser%20%5C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20--generate-ssh-keys%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%222%22%3E%0A%3CLI%3EOpen%20port%205432%20on%20Network%20Security%20Group%20(NSG)%20on%20Ubuntu%20VM%20so%20PgBouncer%20can%20listen%20on%20the%20port%20for%20incoming%20connections%20from%20application.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%3Eaz%3C%2FSPAN%3E%20%3CSPAN%3Evm%3C%2FSPAN%3E%20open-port%3CSPAN%3E%20--port%3C%2FSPAN%3E%20%3CSPAN%3E5432%3C%2FSPAN%3E%3CSPAN%3E%20--resource-group%3C%2FSPAN%3E%20myResourceGroup%3CSPAN%3E%20%E2%80%93name%3C%2FSPAN%3E%20PgBouncerPoolVM%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EImportant%20Note%3A%20%26nbsp%3B%3C%2FSTRONG%3EThe%20above%20command%20will%20create%20an%20NSG%20for%20the%20VM%20and%20open%20port%205432%20on%20the%20VM%20for%20%3CSTRONG%3EAny%20Source%20IP%3C%2FSTRONG%3E.%20This%20is%20%3CSTRONG%3Enot%20recommended%3C%2FSTRONG%3E%20from%20security%20perspective.%20Ideally%2C%20the%20VM%20should%20be%20configured%20in%20a%20VNet%20and%20the%20Source%20IP%20range%20must%20be%20defined%20and%20whitelisted%20in%20NSG%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Fnetwork%2Fnsg%3Fview%3Dazure-cli-latest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eaz%20network%20nsg%3C%2FA%3E%20to%20restrict%20access%20to%20PgBouncer%20VM%20to%20client%20application%20VMs%20only.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%223%22%3E%0A%3CLI%3ESsh%20into%20the%20VM%20and%20install%20PgBouncer.%20You%20can%20leverage%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsecurity-center%2Fsecurity-center-just-in-time%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ejust%20in%20time%20secure%3C%2FA%3E%20access%20to%20login%20to%20VM%20to%20configure%20PgBouncer.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bssh%20%3CA%20href%3D%22mailto%3Apgadminuser%4052.229.29.56%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epgadminuser%40%3C%2FA%3E%3CPUBLICIPENDPOINT%3E%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bsudo%20apt-get%20install%20-y%20pgbouncer%3C%2FPUBLICIPENDPOINT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%224%22%3E%0A%3CLI%3EDownload%20the%20certificate%20file%20from%20the%20Certificate%20Authority%20to%20connect%20to%20Azure%20DB%20for%20PostgreSQL%20using%20SSL%20connection.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bsudo%20wget%20%3CA%20href%3D%22https%3A%2F%2Fwww.digicert.com%2FCACerts%2FBaltimoreCyberTrustRoot.crt%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.digicert.com%2FCACerts%2FBaltimoreCyberTrustRoot.crt%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bsudo%20openssl%20x509%20-inform%20DER%20-in%20BaltimoreCyberTrustRoot.crt%20-text%20-out%20%2Fetc%2Froot.crt%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%225%22%3E%0A%3CLI%3EModify%20the%20Pgbouncer.ini%20configuration%20file%20located%20in%20%2Fetc%2Fpgbouncer%2FpgBouncer.ini%20using%20the%20following%20settings%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%5Bdatabases%5D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B*%20%3D%20host%3D%3CSERVERNAME%3E.postgres.database.azure.com%26nbsp%3B%20port%3D5432%3C%2FSERVERNAME%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%5Bpgbouncer%5D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%23%20Do%20not%20change%20these%20settings%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Blisten_addr%20%3D%200.0.0.0%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bauth_file%20%3D%20%2Fetc%2Fpgbouncer%2Fuserlist.txt%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bauth_type%20%3D%20trust%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bserver_tls_sslmode%20%3D%20verify-ca%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bserver_tls_ca_file%20%3D%20%2Fetc%2Froot.crt%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%23%20These%20are%20defaults%20and%20can%20be%20configured%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%23%20please%20leave%20them%20as%20defaults%20if%20you%20are%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%23%20uncertain.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Blisten_port%20%3D%205432%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bunix_socket_dir%20%3D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Buser%20%3D%20postgres%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bpool_mode%20%3D%20transaction%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bmax_client_conn%20%3D%20100%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bignore_startup_parameters%20%3D%20extra_float_digits%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Badmin_users%20%3D%20postgres%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%226%22%3E%0A%3CLI%3EModify%20the%20authentication%20file%20located%20in%20%2Fetc%2Fpgbouncer%2Fuserlist.txt%20to%20specify%20the%20username%2Fpassword%20pairs%20that%20clients%20may%20use%20to%20access%20PgBouncer%20service.%20Each%20line%20should%20be%20in%20format%20below%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%22username%40hostname%22%20%22password%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BEach%20entry%20is%20simply%20new%20line%20separated%20for%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%22sa%40mypgserver%22%20%22P%40ssword1234%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%22test%40mypgserver%22%20%22Test%40%231234%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3A%20Azure%20Database%20for%20PostgreSQL%20usernames%20are%20always%20in%20the%20format%20username%40hostname%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%227%22%3E%0A%3CLI%3EStart%20the%20PgBouncer%20service%20and%20verify%20there%20are%20no%20errors%20in%20the%20log%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20sudo%20service%20pgbouncer%20start%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20more%20%2Fvar%2Flog%2Fpostgresql%2Fpgbouncer.log%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%228%22%3E%0A%3CLI%3EIf%20postgresql-client%20is%20not%20installed%20on%20the%20VM%2C%20install%20postgresql-client%20and%20use%20psql%20to%20validate%20the%20connectivity%20to%20PgBouncer%20service%20which%20in-turn%20would%20connect%20to%20backend%20Azure%20DB%20for%20PostgreSQL%20service.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20sudo%20apt-get%20update%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20sudo%20apt-get%20install%20postgresql-client%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20psql%20-h%20127.0.0.1%20-p%205432%20-U%20sa%40mypgserver%20-d%20postgres%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%229%22%3E%0A%3CLI%3EFrom%20external%20application%20VM%20or%20workstation%20connect%20to%20PgBouncer%20service%20to%20make%20sure%20your%20connection%20is%20successful.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20psql%20-h%20%3CPUBLICIPENDPOINT%3E%20-p%205432%20-U%20sa%40mypgserver%20-d%20postgres%3C%2FPUBLICIPENDPOINT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20are%20now%20all%20setup%20to%20leverage%20PgBouncer%20connection%20pooling%20proxy%20to%20connect%20to%20Azure%20DB%20for%20PostgreSQL%20service.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--761654500%22%20id%3D%22toc-hId--761654500%22%3EDeploying%20Pgbouncer%20using%20QuickStart%20ARM%20template%3C%2FH2%3E%0A%3CP%3EIf%20you%20would%20like%20to%20skip%20the%20above%20steps%20and%20want%20to%20quickly%20setup%20and%20test%20PgBouncer%20with%20Azure%20DB%20for%20PostgreSQL%2C%20you%20can%20click%20on%20Deploy%20button%20below.%20It%20will%20take%20you%20to%20the%20template%20and%20once%20you%20provide%20the%20parameter%20values%2C%20it%20will%20provision%20an%20Ubuntu%20VM%20with%20PgBouncer%20installed%2C%20setup%20and%20running%20connected%20to%20Azure%20Database%20for%20PostgreSQL.%20I%20would%20encourage%20you%20to%20give%20it%20a%20try.%20The%20template%20is%20available%20in%20our%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fblob%2Fmaster%2Farm-templates%2FExampleWithPgBouncer%2FREADME.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGitHub%20repository%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%23create%2FMicrosoft.Template%2Furi%2Fhttps%253A%252F%252Fraw.githubusercontent.com%252FAzure%252Fazure-postgresql%252Fmaster%252Farm-templates%252FExampleWithPgBouncer%252Ftemplate.json%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20161px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F121743iF6618A5E64207F82%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22deploybutton%22%20title%3D%22deploybutton%22%20%2F%3E%3C%2FSPAN%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20you%20have%20deployed%20the%20ARM%20Template%20**successfully%20without%20any%20errors**%2C%20you%20will%20be%20able%20to%20see%20the%20hostname%2C%20sshCommand%20and%20psqlCommand%20as%20part%20of%20Deployment%20Outputs%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F121739iBC9CBCDDD02948D9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Microsoft.Template.jpg%22%20title%3D%22Microsoft.Template.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Ehostname%3C%2FSTRONG%3E%20is%20the%20Public%20DNS%20for%20the%20Ubuntu%20VM%20hosting%20pgbouncer%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EsshCommand%3C%2FSTRONG%3E%20provides%20the%20ssh%20command%20to%20connect%20to%20the%20Ubuntu%20VM%20hosting%20pgbouncer%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EpsqlCommand%3C%2FSTRONG%3E%20provides%20the%20psql%20command%20to%20connect%20the%20PostgreSQL%20Server%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Epsql%20%22host%3D%7BdnsLabelPrefix%7D.%7Bregion%7D.cloudapp.azure.com%20port%3D5432%20dbname%3D%7Byour_database%7D%20user%3D%7Byour_username%7D%22%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EFor%20Example%20%3C%2FSTRONG%3E%3A%20psql%20%22host%3Dpgubuntu.westus.cloudapp.azure.com%20port%3D5432%20dbname%3Dpostgres%20user%3Dpguser%40pgbouncerserver%22%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20this%20helps%20!!!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ERamkumar%20Chandrasekaran%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ESenior%20Software%20Engineer%2C%20Microsoft%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EParikshit%20Savjani%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ESenior%20Program%20Manager%2C%20Microsoft%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-730555%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20blog%20post%2C%20we%20share%20the%20steps%20to%20install%20and%20setup%20PgBouncer%20connection%20pooling%20proxy%20between%20application%20and%20Azure%20DB%20for%20PostgreSQL%20to%20reap%20the%20performance%20and%20resiliency%20benefits%20it%20offers.%20In%20addition%2C%20we%20also%20provide%20you%20with%20a%20QuickStart%20template%20at%20the%20end%20of%20the%20blog%20post%20which%20can%20enable%20you%20to%20quickly%20deploy%20and%20setup%20a%20PgBouncer%20in%20a%20VM%20and%20get%20started.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E

In my previous blog post on Azure blogs, I explain why it is important to leverage connection pooling proxy with PostgreSQL and how using a connection pooling proxy like PgBouncer against Azure Database for PostgreSQL can provide a significant performance boost, assuming connection pooling is not used at the application layer. To give some estimates of the performance improvement when using PgBouncer for connection pooling with Azure Database for PostgreSQL, we ran a simple performance benchmark test with pgbench. pgbench provides a configuration setting to create new connection for every transaction so we leveraged that to measure the impact of connection latency on throughput of the application. The following are the observations with A/B testing comparing throughput with standard pgbench benchmark testing with and without PgBouncer. In the test, we ran pgbench with scale factor of 5 against Azure Database for PostgreSQL running on general purpose tier with 2 vCores (GP_Gen5_2). The only variable during the tests was PgBouncer. With PgBouncer, the throughput improved 4x times as shown below while connection latency was reduced by 40 percent.

 

PerfImprovementwithPgBouncer.png

 

In this blog post, we share the steps to install and setup PgBouncer connection pooling proxy between application and Azure DB for PostgreSQL to reap the performance and resiliency benefits it offers. In addition, we also provide you with a QuickStart template at the end of the blog post which can enable you to quickly deploy and setup a PgBouncer in a VM and get started. 

 

PgBouncer connection proxy is setup between the application and database layer as shown in the image below. As Azure DB for PostgreSQL is a fully managed platform service, you won’t have access to install any external component on database server. In this case, if you are running your application on an Azure VM, you can setup PgBouncer on the same VM running the application. If the application runs on a managed service like Azure App Services or Azure Functions, you may have to provision a separate Ubuntu VM to run PgBouncer proxy service. However, if you are running your application on Azure Kubernetes Services (AKS), you can leverage PgBouncer sidecar to run it as a Kubernetes sidecar to your application. In this blog post, we will focus on steps to install and run PgBouncer on Ubuntu VM while steps to setup PgBouncer sidecar can be a topic for a follow-up blog post.

Connection_pooling.jpg

 

Steps to setup PgBouncer on Ubuntu VM

All the steps mentioned below uses Azure CLI to provision and deploy resources.

  1. Provision an Ubuntu VM. If your application is already running on Azure VM and you have a VM provisioned, you can skip this step and move to step 2.

          az group create --name myResourceGroup --location eastus

          az vm create \

          --resource-group myResourceGroup \

          --name PgBouncerPoolVM \

          --image UbuntuLTS \

          --admin-username pgadminuser \

          --generate-ssh-keys

 

  1. Open port 5432 on Network Security Group (NSG) on Ubuntu VM so PgBouncer can listen on the port for incoming connections from application.

          az vm open-port --port 5432 --resource-group myResourceGroup –name PgBouncerPoolVM

 

Important Note:  The above command will create an NSG for the VM and open port 5432 on the VM for Any Source IP. This is not recommended from security perspective. Ideally, the VM should be configured in a VNet and the Source IP range must be defined and whitelisted in NSG using az network nsg to restrict access to PgBouncer VM to client application VMs only.

 

  1. Ssh into the VM and install PgBouncer. You can leverage just in time secure access to login to VM to configure PgBouncer.

         ssh pgadminuser@<PublicIPEndpoint>
         sudo apt-get install -y pgbouncer

 

  1. Download the certificate file from the Certificate Authority to connect to Azure DB for PostgreSQL using SSL connection.

         sudo wget https://www.digicert.com/CACerts/BaltimoreCyberTrustRoot.crt

         sudo openssl x509 -inform DER -in BaltimoreCyberTrustRoot.crt -text -out /etc/root.crt

 

  1. Modify the Pgbouncer.ini configuration file located in /etc/pgbouncer/pgBouncer.ini using the following settings

         [databases]

         * = host=<servername>.postgres.database.azure.com  port=5432

   

        [pgbouncer]

         # Do not change these settings:

         listen_addr = 0.0.0.0

         auth_file = /etc/pgbouncer/userlist.txt

         auth_type = trust

         server_tls_sslmode = verify-ca

         server_tls_ca_file = /etc/root.crt

             

         # These are defaults and can be configured

         # please leave them as defaults if you are

         # uncertain.

         listen_port = 5432

         unix_socket_dir =

         user = postgres

         pool_mode = transaction

         max_client_conn = 100

         ignore_startup_parameters = extra_float_digits

         admin_users = postgres

 

  1. Modify the authentication file located in /etc/pgbouncer/userlist.txt to specify the username/password pairs that clients may use to access PgBouncer service. Each line should be in format below

         "username@hostname" "password"

         

         Each entry is simply new line separated for example:

         "sa@mypgserver" "P@ssword1234"

         "test@mypgserver" "Test@#1234"

 

Note: Azure Database for PostgreSQL usernames are always in the format username@hostname

 

  1. Start the PgBouncer service and verify there are no errors in the log

          sudo service pgbouncer start

          more /var/log/postgresql/pgbouncer.log

 

  1. If postgresql-client is not installed on the VM, install postgresql-client and use psql to validate the connectivity to PgBouncer service which in-turn would connect to backend Azure DB for PostgreSQL service.

        sudo apt-get update

        sudo apt-get install postgresql-client

        psql -h 127.0.0.1 -p 5432 -U sa@mypgserver -d postgres

 

  1. From external application VM or workstation connect to PgBouncer service to make sure your connection is successful.

        psql -h <PublicIPEndpoint> -p 5432 -U sa@mypgserver -d postgres

 

You are now all setup to leverage PgBouncer connection pooling proxy to connect to Azure DB for PostgreSQL service.

 

Deploying Pgbouncer using QuickStart ARM template

If you would like to skip the above steps and want to quickly setup and test PgBouncer with Azure DB for PostgreSQL, you can click on Deploy button below. It will take you to the template and once you provide the parameter values, it will provision an Ubuntu VM with PgBouncer installed, setup and running connected to Azure Database for PostgreSQL. I would encourage you to give it a try. The template is available in our GitHub repository

 

deploybutton

 

Once you have deployed the ARM Template **successfully without any errors**, you will be able to see the hostname, sshCommand and psqlCommand as part of Deployment Outputs  

 

Microsoft.Template.jpg

 

hostname is the Public DNS for the Ubuntu VM hosting pgbouncer

sshCommand provides the ssh command to connect to the Ubuntu VM hosting pgbouncer

psqlCommand provides the psql command to connect the PostgreSQL Server  

 

psql "host={dnsLabelPrefix}.{region}.cloudapp.azure.com port=5432 dbname={your_database} user={your_username}" 

For Example : psql "host=pgubuntu.westus.cloudapp.azure.com port=5432 dbname=postgres user=pguser@pgbouncerserver"  

 

Hope this helps !!!

 

Ramkumar Chandrasekaran

Senior Software Engineer, Microsoft

 

Parikshit Savjani

Senior Program Manager, Microsoft