Deploying pgAdmin 4 in server mode with SSO

A planning and build guide for running pgAdmin 4 as a shared web tool with single sign-on through Keycloak, the OAUTH2_CONFIG setting that most setups get wrong, and per-user control of which PostgreSQL servers appear.

On this page

Deploying pgAdmin 4 in server mode with SSO for a shared team

DBAs and developers often need one browser tool for PostgreSQL without every person sharing a desktop login or a local copy of server credentials. The supported way to get there is pgAdmin 4 in server mode with OAuth2 or OIDC login, and in this build Keycloak is the identity provider. In server mode, pgAdmin users must log in to a central web application, an initial superuser account is created at setup, and the identity provider handles who can sign in while pgAdmin handles what each signed-in user can see. Keycloak is the identity provider this integration is built around, and the pages on Keycloak describe how to run it in your own infrastructure.

Choose the application server before you write any configuration. pgAdmin can run as a WSGI application under Apache HTTPD with mod_wsgi, standalone under uWSGI or Gunicorn, or behind NGINX as a reverse proxy in front of one of those. Your team's existing web standards, TLS termination point and process supervision usually decide this. Whichever you pick, the decision affects how the OAUTH2_CONFIG settings reach the process, so confirm the exact configuration file location and service user with the version of pgAdmin you intend to deploy before you wire up the identity provider.

The integration gotcha to plan for is that pgAdmin reads OAuth2 and OIDC provider settings only from the OAUTH2_CONFIG list. Individual PGADMIN_CONFIG_OAUTH2_* environment variables do not configure a provider, so a deployment that sets them and expects a working login will fail quietly. Write the provider entry once, in the configuration file that your chosen server loads, then test a login with a non-administrator account before you tell users the tool is ready. Confirm the redirect URI and client settings in Keycloak match that entry exactly.

What you need before configuring pgAdmin 4 and Keycloak

Four components have to be in place before any login flow works: a pgAdmin 4 deployment in server mode, a Keycloak realm with a client for pgAdmin, supporting databases and certificates, and network paths that match who connects to what. Confirm each item in the list below against your environment before you change any configuration.

pgAdmin 4 version and server mode

Pick the pgAdmin 4 release you will run, then confirm its server deployment options against the official documentation for that release. Server mode runs pgAdmin as a shared web application under Apache with mod_wsgi, under uWSGI or Gunicorn, or behind NGINX. Choose one runtime now, because the reverse proxy, process user and file paths all depend on it. Also confirm the OAuth2 options for your exact version, since the settings can change between releases.

Server deployment docs

Keycloak release and realm

If you self-host Keycloak, plan on the current release, 26.8.1, and confirm it against the official release notes before you install. Create a dedicated realm for the pgAdmin login with its own users, groups and a confidential client for pgAdmin. Keep this realm separate from production realms used by other applications so that client secrets and role mappings stay contained.

Keycloak project

Production database for Keycloak

Keycloak's development file database is for testing only, so plan a production database before go-live. PostgreSQL 14 to 18 is a supported choice. Decide who owns the instance, how it is backed up, and whether it is highly available, because Keycloak sign-in depends on it. Confirm the supported database list for your Keycloak release in the database guide.

Keycloak database guide

TLS certificates for both hostnames

Plan certificates for the pgAdmin hostname and the Keycloak hostname, each issued by a CA your users' browsers and the pgAdmin server trust. The OIDC redirect and token exchange fail when the certificate chain is incomplete or when the pgAdmin server does not trust the Keycloak CA. Record the expiry dates and the renewal owner for both certificates before the first test login.

Network paths for users and servers

Map two paths. The first runs from user browsers to pgAdmin and from browsers to Keycloak. The second runs from the pgAdmin host to Keycloak for token and metadata calls, and from pgAdmin to every target PostgreSQL server it will manage. Confirm firewall rules, DNS names and proxy settings for each path, and confirm that the pgAdmin host can reach the database ports it needs.

Target servers and admin accounts

List the PostgreSQL servers that pgAdmin should show, the database roles that will connect, and the initial superuser account pgAdmin creates in server mode. Decide who holds that account and how its credentials are stored. Plan for the server list to be controlled per user or group, since visibility in pgAdmin is the access boundary the team will rely on.

Configuring pgAdmin 4 server mode with OIDC login, step by step

This sequence takes pgAdmin 4 from a bare WSGI host to a working server-mode instance where users sign in through Keycloak and see only the PostgreSQL servers their groups are allowed to use.

  1. Choose and prepare the WSGI host

    Your platform team picks how pgAdmin will run: under Apache HTTPD with mod_wsgi, standalone with uWSGI or Gunicorn, or behind NGINX as the front end. The choice depends on what your team already operates and how your reverse proxy handles TLS. Set up the host with a fixed DNS name, a TLS certificate and a reverse proxy that forwards requests to the application process. Done means the proxy answers on the pgAdmin hostname over HTTPS, with the application process not yet reachable directly from users.

  2. Install pgAdmin 4 in server mode

    The platform engineer installs pgAdmin 4 on the host and runs it in server mode rather than desktop mode, so users must log in through the web interface. Install the application into a dedicated Python environment owned by a service account, and confirm the process starts without error. Done means the login page loads through the proxy and the process log shows no configuration warnings. Note that server mode requires an initial superuser account, which the next steps create.

  3. Register the pgAdmin client in Keycloak

    In your Keycloak realm, the identity administrator creates an OpenID Connect client for pgAdmin, sets the client to require a secret, and enters redirect URIs that match the exact pgAdmin hostname and callback path used by the proxy. Mismatched scheme, port or path is the most common reason for a failed redirect, so copy the URIs from the pgAdmin OAuth2 documentation for your version. Add a group membership mapper so the groups claim appears in the ID token. Done means a test user's token, inspected in a Keycloak client scope preview, contains the expected groups claim.

  4. Write OAUTH2_CONFIG as a list in config_local.py

    The administrator adds an OAUTH2_CONFIG list to config_local.py, with one entry for the Keycloak provider containing the OIDC metadata URL, client ID, client secret, scopes and the claim used for the user name. Individual PGADMIN_CONFIG_OAUTH2_* environment variables do not configure a provider, so settings placed only in environment variables will be ignored. Keep the client secret out of the file where possible by sourcing it from your secrets store at deploy time. Done means pgAdmin starts, the login page shows a Keycloak sign-in option, and the startup log names the configured provider.

  5. Create the initial superuser and confirm login

    The administrator creates the initial pgAdmin superuser with the setup command, using a separate break-glass account that is not tied to a single person's identity. Then signs in once through Keycloak as a test user and once with the superuser account. Watch the proxy and application logs during the redirect to confirm the callback returns to the pgAdmin hostname and the user lands on the dashboard. Done means both logins succeed, and the test user's account appears in the pgAdmin user list with the expected identity. If the callback fails, check the redirect URIs and the client secret before changing anything else.

  6. Register PostgreSQL servers and assign access

    The DBA registers each PostgreSQL server in pgAdmin using a connection account that has only the privileges that tool users need, not a shared superuser password. Access to each server is then limited to the intended users or groups, so the test user sees only the servers their groups should reach. Confirm the server list for a test user in each group against your access matrix, and compare it with what a user outside those groups sees. Done means each test user's server list matches the matrix, and the connection attempts appear in the PostgreSQL logs under the expected account.

Choosing the web server: Apache with mod_wsgi, uWSGI, Gunicorn or NGINX

The pgAdmin server deployment documentation describes running pgAdmin as a WSGI application under Apache HTTPD with mod_wsgi. This suits teams whose operations standard is already Apache, with its virtual host configuration, certificate handling and module management. Apache serves TLS and hands requests to the Python application, so the web server layer stays familiar to administrators who already maintain other Apache sites on the same host.

  • TLS termination: Apache can terminate HTTPS directly, so certificates and cipher policy live in the same configuration your security team already reviews.
  • Process management: mod_wsgi manages the Python worker processes inside Apache, so there is one service to monitor rather than a separate application server.
  • Existing standards: If your hosts already run Apache hardening baselines and logging, pgAdmin inherits them without a new pattern.
  • Linux host fit: The package and module versions must match your distribution's Apache and Python packages, which should be confirmed before the build starts.
  • Operational cost: Teams without Apache experience take on configuration work that is not specific to pgAdmin.

Confirm with your operations team whether Apache is the approved front end for internal tools, and check the current pgAdmin documentation for the supported mod_wsgi configuration before you write the virtual host.

Why OAUTH2_CONFIG must be a list, and what breaks when it is not

pgAdmin reads OAuth2 and OIDC provider settings only from the OAUTH2_CONFIG list. Individual PGADMIN_CONFIG_OAUTH2_* environment variables do not define a provider, so setting client ID, client secret, server metadata URL and scopes as separate variables produces a configuration pgAdmin never loads. The usual symptom is that the login page shows only the built-in internal login, or that the expected provider button is missing, with no error that points at the variables. Teams often discover this after a container rebuild or a Helm values change, because the variables looked correct in the deployment manifest. Before you troubleshoot the identity provider, confirm that the provider definition lives inside OAUTH2_CONFIG in the config_system.py or config_local.py file that your deployment actually reads, and that the redirect URI registered at Keycloak or your other provider matches what pgAdmin generates.

To verify what pgAdmin has loaded, sign in as the initial superuser and check which authentication sources appear on the login page, then compare that with the file on the running container or host, not the file in your repository. Inspect the effective configuration inside the running process, since a mounted file, an environment override or a stale image can make the two disagree. Keep a short, written check with the expected provider name, the metadata URL and the callback path, and run it in staging before each change. Also confirm the values reach pgAdmin without leaking the client secret into logs or shell history, because a secret passed on the command line is visible to anyone who can read the process list.

Review this configuration after every pgAdmin upgrade, not only after changes to your identity provider. The pgAdmin OAuth2 documentation is the reference to check for any change in how the list is read, and your upgrade test should include a sign-in through each provider, a sign-in with the superuser account and a check that group or claim mapping still behaves as intended. Record the pgAdmin version, the image digest and the date of each successful check, so an auditor or a colleague can see when the login path was last proven to work. If the check fails after an upgrade, the internal superuser login is your recovery path, so confirm it still works and that its credentials are stored under your normal secrets process.

How a pgAdmin login travels from browser to Keycloak and back

The login starts in the browser, which reaches the reverse proxy (NGINX or Apache) in front of pgAdmin running in server mode. pgAdmin reads its OAuth2 provider from the OAUTH2_CONFIG list, so it redirects the browser to Keycloak for sign-in and receives the identity back over OIDC. Only after pgAdmin has a session does it open connections to the PostgreSQL servers, which sit on the database network behind the trust boundary. Check that the redirect URIs match the public hostname, and that each server a user can see is registered in pgAdmin.

Login and database paths across the client, application and database networks.

Mapping Keycloak claims and groups to pgAdmin users and server access

This table compares the identity values Keycloak can send in tokens with what pgAdmin needs from them: a stable user identity, a contact address, and a way to decide which server registrations each person sees. Claim names are illustrative, so confirm them in your own Keycloak realm and pgAdmin release before you rely on a row. The main takeaway is that pgAdmin login and server visibility are separate decisions, and the second one needs its own design.

Identity inputTypical source in KeycloakRole in pgAdminWhat to confirm
Subject (sub)Stable user ID issued by the realmIdentifies the pgAdmin account across loginsConfirm which claim pgAdmin uses as the unique user key in your release
EmailEmail claim, mapped from the user profileAccount contact and common login identifierConfirm the claim name and whether it is verified in the realm
Usernamepreferred_username claim (illustrative)Display or fallback identifierConfirm it is unique and cannot be changed by users
Group membershipGroups mapper or client role claim (illustrative name)Input for deciding which server groups a user can openConfirm the mapper emits group paths or names as your design expects
Admin roleRealm or client role (illustrative name, such as pgadmin-admin)Candidate for administrator rights in pgAdminDecide whether admin rights come from the token or are granted inside pgAdmin
Server registrationNot a token value, managed inside pgAdminShared or restricted per user or groupTest that a user outside the group cannot see or connect to the server

Hardening the pgAdmin login path and the database access behind it

A shared pgAdmin instance is a single door into every registered database, so the web tier, the login path and the PostgreSQL roles behind each server need to be hardened together. The cards below cover what to confirm at each layer and who owns each check.

Encrypt the pgAdmin hostname and back-end hops

Serve pgAdmin only over HTTPS on its public or internal hostname, and confirm that the reverse proxy (NGINX, Apache or the WSGI server) terminates TLS with current certificates and redirects plain HTTP. Then check the second hop: traffic from the proxy to the pgAdmin process and the connection from pgAdmin to each PostgreSQL server should both use TLS, with sslmode set on each registered server. Ask your team to confirm which hops are encrypted and which certificate authority signs them.

pgAdmin server deployment

Restrict pgAdmin to a management network

pgAdmin is an administrative tool, so it should not be reachable from the open internet or from general office networks. Place it behind a VPN, bastion or identity-aware proxy, and allow inbound traffic only from the subnets your DBAs and developers use. Verify the restriction from outside the allowed range, because a firewall rule that looks correct on paper is often bypassed by a second ingress path.

Control or disable the initial superuser

Server mode creates an initial superuser account during setup. Decide who holds that credential, store it in your secrets manager, and either disable it once SSO works or restrict it to a break-glass procedure with logged use. Confirm that day-to-day administration happens through named accounts mapped to your identity provider, so every action traces to a person.

Scope PostgreSQL roles behind each server

pgAdmin logs in to PostgreSQL with whatever role is saved for each registered server, so a shared administrative role gives every pgAdmin user full control. Create least-privilege roles per team, grant only the schemas and operations each group needs, and avoid superuser roles for routine work. On the database side, PostgreSQL 18 added an OAuth 2.0 authentication method, and MD5 password authentication is deprecated in favor of SCRAM, so confirm that pg_hba.conf uses SCRAM or the OAuth method for these roles.

PostgreSQL auth methods

Set session timeouts and idle limits

A pgAdmin browser session left open on a shared workstation exposes every server the user can see. Review the session lifetime and idle timeout settings in pgAdmin's configuration and align them with your security policy, then confirm that Keycloak session and token lifetimes match. Ask the implementation team to show how a logged-out or expired session stops access to open server connections.

Plan pgAdmin and Keycloak patching

pgAdmin, its WSGI server, the reverse proxy and Keycloak all need updates, and each one can carry a security fix. Set a fixed review cadence for each component, test upgrades in a non-production copy that uses the same OAUTH2_CONFIG, and keep Keycloak operator updates on manual approval so upgrades happen when the team chooses. Record which versions run where, and confirm who signs off on each change.

Keycloak upgrading guide

Controlling which PostgreSQL servers each user can see

Decide first whether every signed-in user sees the same server list or whether visibility follows group membership. A shared list is quicker to run and suits a small team where everyone needs the same read access, but it means a developer who works on one project also sees every database in the estate. Scoping by group matches how DBAs and application teams usually divide ownership and keeps the list short enough that people pick the right server. Write down the mapping from identity provider groups to pgAdmin groups before the build, because the same groups should later drive database roles.

Treat the pgAdmin server list as a usability feature only. A user who cannot see a server in pgAdmin may still reach it if that server's saved credentials are shared, if a service account is used for several groups, or if the database accepts connections from the pgAdmin host by another route. Enforcement belongs in PostgreSQL: one login role per group, CONNECT privileges granted only to the roles that need a given database, and pg_hba.conf rules that name the database, the role, the allowed source network and an encrypted method such as scram-sha-256. The PostgreSQL authentication methods reference explains how these rules are matched. Then test each role with psql from the pgAdmin host to confirm the denial happens at the database.

Before relying on any sharing or grouping feature, confirm how the pgAdmin release you run stores server definitions and whether it can limit a server to specific users or groups. This page does not assume a particular behavior, so check the server deployment documentation for your version. Test with two accounts in different groups and confirm that each sees only its own servers, that stored passwords are not visible to the other group, and that the administrator account can see everything needed for audit. Also confirm whether users can add their own server connections, and decide whether that is allowed. Record the results with the configuration so the same tests can be repeated after each pgAdmin upgrade.

Common failures when pgAdmin SSO does not work

This table pairs each common pgAdmin OAuth2 or OIDC login failure with its likely cause and the first check to run. Work down the rows in order when a login fails, because several symptoms look identical from the browser but have different causes on the server.

FailureLikely causeFirst check
Redirect URI mismatch at KeycloakThe valid redirect URI registered in the Keycloak client does not exactly match the callback URL pgAdmin sends, including scheme, host, port and path.Compare the redirect URI in the Keycloak client settings with the pgAdmin callback URL character by character.
OAUTH2_CONFIG not loadedpgAdmin ignores provider settings set only as individual PGADMIN_CONFIG_OAUTH2_* environment variables, so no provider is registered.Confirm the provider is defined in the OAUTH2_CONFIG list in the pgAdmin configuration file and that the file is the one the process actually reads.
OAUTH2_CONFIG malformedA syntax error, missing comma, unclosed bracket or wrong key name stops the list from being parsed or drops the provider.Check pgAdmin startup logs for configuration errors and validate the Python syntax of the config file before restarting.
Wrong issuer or discovery URLThe issuer points to the wrong realm, a trailing slash differs, or the discovery document path is incorrect for the Keycloak realm.Open the realm's OpenID discovery document in a browser and confirm the issuer value matches the configured one exactly.
TLS trust for the Keycloak certificatepgAdmin's Python process does not trust the internal certificate authority that signed the Keycloak endpoint, so token and metadata requests fail.Test the discovery URL from the pgAdmin host with the same CA bundle the process uses, and confirm the CA is available to that runtime.
Clock skew between hostsToken validity checks fail when the pgAdmin host and Keycloak clocks drift apart, so valid tokens look expired or not yet valid.Compare UTC time on both hosts and confirm both synchronize with the same time source.
Missing groups or claimsThe identity provider does not send the group or email claim pgAdmin expects, or the claim name in the configuration does not match.Decode a test token or review the user info response and confirm the claim names and values match the pgAdmin settings.
Reverse proxy headersNGINX or another proxy does not forward the original host, scheme or port, so pgAdmin builds the wrong callback URL or rejects the session.Inspect the proxy configuration for forwarded host and protocol headers and compare the URL pgAdmin generates with the public address users type.
Servers not visible to a userServer group or ownership settings in pgAdmin do not grant that user access to the saved server entries, even though login succeeds.Log in as the affected user, check which server groups are assigned to them, and compare with an account that can see the servers.

How long, how much effort and what it needs from your team

What drives the cost and effort of a pgAdmin 4 server mode with SSO deployment?

The main cost driver is how much of the identity and proxy stack already exists. Effort rises with the number of pgAdmin deployment steps that are new to your team: choosing the WSGI or reverse proxy setup, building the OAuth2 provider configuration, mapping user groups, and deciding which PostgreSQL servers each group can see. Access model complexity matters too. A single group with one database is far less work than several teams with separate server lists and different privileges. Cost is mostly staff time, paid either to your engineers or to a partner under a fixed-scope written agreement. Most of the spend is one-time build work, with recurring costs for pgAdmin upgrades, identity provider updates and access reviews.

Can our internal team deploy pgAdmin 4 with SSO without outside help?

Yes, if your team already runs a reverse proxy and an OIDC identity provider and has someone comfortable with Python web application deployment. The hardest part is usually not pgAdmin itself but the OAuth2 configuration. pgAdmin reads provider settings only from the OAUTH2_CONFIG list, so an engineer must confirm that before testing login. Teams without prior experience with OIDC clients, redirect URIs, token claims and TLS termination will spend more time on these points. An outside partner can take over the build under a written scope, or review your design before you roll it out. Whichever route you choose, confirm who owns the initial superuser account and the server definitions.

How long does the work take if we already run Keycloak and a reverse proxy?

Teams with a working Keycloak realm and a reverse proxy that already terminates TLS usually need the least time, because the main work is the pgAdmin client registration, the OAUTH2_CONFIG entry, and group-based access testing. If Keycloak is new, the schedule grows with the identity design: realms, clients, group claims, administrator roles, and the high availability architecture you need. If the proxy is new, add time for certificates, header handling, and the WSGI or uWSGI service configuration. Ask any partner for a timeline that lists these items separately, and confirm it against your own environment, since no fixed duration applies to every setup.

What does the work need from our identity team?

The identity team usually provides the OIDC client for pgAdmin, the redirect URI registered for your public hostname, and the claims that pgAdmin will read. They also decide how groups or roles map to pgAdmin access, and how users are provisioned and removed. Ask them to confirm that the client secret is stored outside the configuration file and that session lifetimes match your policy. If you run Keycloak, check the upgrade model: Keycloak plans four minor releases a year, and the operator documentation strongly recommends manual approval for operator updates. Those decisions affect recurring effort, so include them in the scope.

What does the work need from our database team?

The database team owns the PostgreSQL roles and the servers that pgAdmin will show. They should decide which databases each group can reach, which accounts pgAdmin uses to connect, and how those accounts are audited. Be clear about whether pgAdmin users will reuse their own database credentials or whether shared service accounts are used, because that choice shapes access reviews. The database team also confirms network paths from the pgAdmin host to each PostgreSQL server, and any TLS requirements. Where pgAdmin server definitions are shared, document who may add or edit them.

What should we ask for in a written quote for this integration?

Ask for a written scope that lists each deliverable: the deployment method (Apache with mod_wsgi, uWSGI or Gunicorn behind NGINX), the OAuth2 and OIDC configuration, the group-to-server access model, the test cases for login and denied access, and the handover documents. Ask which items are one-time and which recur, such as pgAdmin upgrades and retesting after identity changes. Ask who provides the identity provider, the reverse proxy, and the TLS certificates, and what happens if one of those is not ready. Finally, ask how changes to scope are priced and approved. DeployOpen does not publish a price list, and a quote follows from the written scope.

What recurring work should we budget for after go-live?

Plan for recurring effort in four areas. First, pgAdmin itself needs version updates and regression checks after each release. Second, the identity provider and its client settings change over time, so login tests should run after each upgrade. Third, server lists and group mappings drift as databases are added or teams change, so access reviews belong on a schedule. Fourth, the proxy and TLS certificates need renewal and monitoring. Ask your partner whether support or a retainer covers these items, and what your own team must do between visits. Ongoing support is available as a retainer, and its terms follow from the written scope.

How DeployOpen scopes and delivers pgAdmin 4 with SSO

DeployOpen plans, builds, tests and hands over pgAdmin 4 in server mode inside your own infrastructure, with login wired to your identity provider and server access defined before users arrive. Engagements are fixed-scope, so the scope, deliverables and timeline are agreed in writing before work starts.

How we scope

  • We confirm which PostgreSQL servers, environments and user groups the shared pgAdmin instance must reach.
  • We record the chosen WSGI or reverse proxy stack, the identity provider and the hosting environment in the written scope.
  • We agree how server visibility will map to teams, roles and environments before any configuration begins.

How we build

  • We deploy pgAdmin 4 under the agreed application server and NGINX setup, with TLS and the initial superuser handled deliberately.
  • We configure OAUTH2_CONFIG as a single provider definition and verify login, logout and group claims against your identity provider.
  • We test that users see only the server definitions their group allows, and that access changes take effect as designed.

How we hand over

  • You receive the configuration, deployment notes and the server-to-group access model in documents your team can review.
  • We walk your DBAs and platform engineers through login troubleshooting, upgrade steps and where each setting lives.
  • The retest results and any open items are recorded so your security and compliance reviewers can trace each control.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers after handover.
  • Retainers can cover pgAdmin version upgrades, identity provider changes and periodic access reviews of server visibility.
  • Any change to the written scope is agreed in writing before the work is performed.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.