PostgreSQL password policies and account lockout

PostgreSQL checks very little about password quality, expiry or failed logins out of the box. This page explains what the server enforces, what you must add, and when to push policy to a directory or SSO.

On this page

What PostgreSQL enforces for passwords and what it leaves to you

PostgreSQL stores and checks passwords, but it has no built-in account lockout, so an auditor who asks how database passwords meet policy will get an answer assembled from several parts. The parts are the passwordcheck module to reject weak choices, VALID UNTIL to expire roles, SCRAM to store password hashes, and an external identity source for everything else. Start by listing which of those parts your policy requires and which team owns each one. Then read the passwordcheck documentation for your PostgreSQL version and confirm exactly which checks it runs, because its rules are narrow and should not be presented to an auditor as a complete complexity policy.

PostgreSQL 18 deprecated MD5 password authentication in favor of SCRAM, so any role that still stores an MD5 hash and any pg_hba.conf line that uses md5 is a finding to remediate before the next review. The client authentication methods list also includes LDAP, PAM, OAuth, GSSAPI, SSPI, RADIUS and certificates, and each one moves part of the policy decision to another system. A directory may hold the password and its lockout rules, an identity provider may hold multi-factor and session policy, and a certificate authority may hold key lifetime. For each method you use, confirm which system counts failed logins and enforces the lockout, because PostgreSQL will not do that for you.

For most teams the practical decision is whether database accounts for people should keep passwords at all. Where a directory or identity provider is already the system of record for staff, moving human logins to it means the lockout, expiry and reset rules are enforced once, where the auditor expects to find them. Service accounts still need a separate answer, usually certificates or a secret store with rotation, and VALID UNTIL remains useful for time-limited roles such as contractor access. Write down which accounts use which method, who can change each setting, and how a disabled identity is removed from PostgreSQL so the database never outlives the policy that governs it.

Password controls PostgreSQL offers and when each fits

This table compares the password-related controls available to a self-hosted PostgreSQL cluster, from the passwordcheck module and VALID UNTIL to directory, PAM, OAuth and certificate methods. Read each row for what the control checks, where it runs, what it cannot do and the situation it suits. The main takeaway is that PostgreSQL enforces little password policy on its own, so most lockout and complexity requirements belong in a directory, SSO provider or certificate design.

ControlWhat it checks or enforcesLimits and when it fits
passwordcheck moduleRuns inside the server when a password is set and can reject weak choices based on rules compiled into the moduleOnly checks passwords at set time, cannot expire accounts or lock them after failed logins, and must be installed and loaded; confirm its rules in the docs for your major version
VALID UNTIL on rolesSets a timestamp after which a role cannot log in with a passwordExpiry only, with no warning, no forced change at next login and no history; fits local roles with a known renewal process
SCRAM password storageStores and verifies passwords with the SCRAM mechanism instead of MD5, and MD5 authentication is deprecated in PostgreSQL 18Protects stored credentials in transit and at rest but enforces no complexity or lockout; set password_encryption to SCRAM and confirm the setting for your version
LDAP methodPasses the login to a directory, which applies its own password, lockout and expiry policyPolicy lives in the directory, and roles must still exist in PostgreSQL; the database fails closed if the directory is unreachable, so plan for that outage
PAM methodDelegates authentication to the operating system's PAM stack, which can apply lockout modulesBehavior depends on host configuration and each PAM module, so test lockout on the host itself; fits teams already managing Linux accounts centrally
OAuth 2.0 methodAccepts tokens from an external identity provider, which applies MFA, lockout and session rulesRequires a validator library and issuer matching; PostgreSQL 18 added this method, so confirm support in your version before design
Certificate methodAuthenticates clients by a certificate chain verified against a CA, so no password is sentNeeds an internal CA and rotation process; fits service accounts and administrators better than large human user populations
Kerberos and GSSAPIUses tickets issued by a central realm, so the password is not sent to PostgreSQLPolicy and lockout come from the realm; confirm mapping and keytab handling for your environment before relying on it

How a password login is checked, and where policy can be applied

The login starts with a client connection request, which the server matches against pg_hba.conf to pick an authentication method. For password methods, PostgreSQL checks the password against a stored SCRAM verifier or hands the check to an external service such as LDAP, PAM or OAuth. Policy points sit at password set time through the server-side passwordcheck module, at role expiry through VALID UNTIL, and at the directory, operating system or SSO. Failed-attempt counting does not appear on the core path, so teams should confirm which external component provides lockout before relying on it.

Login path with the points where password policy can be enforced.

Why PostgreSQL leaves failed-login lockout to the edges

PostgreSQL does not lock a role after a set number of failed logins in the core product. A failed authentication is written to the server log, and the server then lets the next attempt proceed, so a password guessing run against a password-based method will keep getting answers unless something outside the database stops it. Plan lockout as a control owned by another layer, and write that ownership down before go-live. The server log is evidence, not a defense, and any alerting built on it is a detection control that still needs a response process.

The practical question is which layer actually counts failures and where that counter resets. For password methods that check against a directory or SSO provider, the directory or identity provider usually holds the counter, so lockout settings belong in that system and the PostgreSQL role is only a consumer of its answer. For methods that go through PAM or operating-system modules, the counter may sit in the operating system and reset on a timer or on a successful login, which can differ from what the database team expects. Network controls such as firewall rate limits or allow lists in front of port 5432 cover the case where neither upstream layer counts. Confirm, for your PostgreSQL version and each pg_hba.conf method in use, which layer counts, what threshold applies, whether a successful login clears the count, and whether a locked account can still connect through another method.

Test the chain before relying on it. Run controlled failed-login attempts from an approved test host against a non-production cluster, then confirm three things: the lockout fires at the layer you named, the database log records the failures with enough detail to attribute them, and the unlock path works for a legitimate user without an administrator resetting the database role by hand. If a log-driven blocker is part of the design, confirm that it reads the same log format your server version produces and that it can be disabled quickly during an incident.

Where password policy should live by environment

People who administer PostgreSQL are the accounts most likely to be targeted, and they are also the accounts where a directory or SSO is most practical. Where your organization can route administrator logins through a central identity provider, PostgreSQL typically does not need to hold a long-lived password for each person. Where it must, PostgreSQL stores a SCRAM verifier in the role, and the rules for complexity, rotation and lockout have to come from your own process because the server does not enforce them.

  • Preferred path: Use Kerberos, LDAP or a certificate method so that the password or key lives in the directory or on a managed device, not in pg_authid.
  • Stored verifiers: If a local password is kept, confirm that password_encryption is set to scram-sha-256 and that MD5 is not used.
  • Expiry: Apply VALID UNTIL only where you have a process to renew it, and check that the renewal is tracked.
  • Lockout: Expect failed-login lockout to come from the directory, the SSO provider or the network layer, not from PostgreSQL itself.
  • Break-glass access: Keep a documented, separately controlled emergency role with monitored use.

Confirm with your identity team which directory or SSO product enforces lockout for database logins in your environment, and test a failed-login sequence end to end.

Configuring passwordcheck and role expiry on a PostgreSQL cluster

This sequence turns on password quality checks and time-limited human accounts on a PostgreSQL cluster you control, and confirms each setting works before it reaches production. It produces a documented configuration, a staging test record and a review process for expiring roles.

  1. Confirm version and authentication rules

    The platform or database engineer records the running major version and reads the authentication rules in pg_hba.conf, noting every method in use and every address or database each rule covers. Check the version against the supported release list in the PostgreSQL versioning policy, since end-of-life releases will not receive fixes for password handling. Confirm that no rule still uses md5 or trust for human or application logins. The step is done when you have a written inventory of rules and a version number that your team has agreed to run. Settings changed later should be compared against this record.

  2. Confirm SCRAM is the stored password type

    Run SHOW password_encryption as a superuser and confirm the value is scram-sha-256. Then query pg_authid or the role views for existing roles to see how their passwords were stored, because a password set before the setting changed may still be in an older format. PostgreSQL 18 deprecated MD5 password authentication in favor of SCRAM, so any role still relying on MD5 needs a new password set under SCRAM. The step is done when the setting is confirmed and every login role has been re-set or verified. Record the roles you re-set so the change can be audited.

  3. Install and load the passwordcheck module

    Install the passwordcheck module from the contrib package for your PostgreSQL build, then read the module documentation for your exact version before changing configuration. Add passwordcheck to shared_preload_libraries in postgresql.conf, keeping any libraries already listed. Changes to shared_preload_libraries take effect only after a server restart, so schedule the restart in a maintenance window and tell the application owners that connections will drop. The step is done when the server restarts cleanly and SHOW shared_preload_libraries lists passwordcheck. Check the server log for load errors before moving on.

  4. Test weak password rejection on staging

    On a staging cluster built from the same configuration, create a test role and attempt to set passwords that a policy reviewer would expect to fail, such as a short password, one that matches the role name, and one from a common-word list. Confirm the server returns an error for each and accepts a long, unique password. Be aware that passwordcheck applies rules fixed in its code, so it does not replace your organization's full policy on length, rotation or reuse. Write the results into the change record, including the exact rejection messages. The step is done when the test cases and outcomes are documented and signed off by the security owner.

  5. Set VALID UNTIL on named human roles

    For each named human role, set an expiry with ALTER ROLE name VALID UNTIL followed by a timestamp chosen by the owning team, and keep a register that lists each role, its owner and the expiry date. Note that VALID UNTIL controls when a password-based login stops working, and it does not remove the role or its privileges, so revocation still needs a separate step at offboarding. Agree a review process in which the register is checked before each expiry date and extensions are approved in writing. Application and service roles should be handled through their own rotation process, not a human expiry cycle. The step is done when every named human role has an expiry and an owner in the register.

  6. Reload configuration and verify with test logins

    Apply the configuration with a reload where the setting allows it, and confirm the values with pg_settings and by checking the log for errors. Then run test logins as a test human role with a valid password, an expired role and a role with a rejected password change to confirm that expiry blocks login and that the error text matches what your support team expects. Confirm that passwords set after the restart are checked by passwordcheck. The step is done when the test logins produce the expected results and the outcomes are stored with the change record. Repeat the same checks on production after the staging sign-off.

Security risks that remain when policy lives only in the database

PostgreSQL enforces very little of a password policy by itself, so the database can look compliant on paper while credentials age, get reused or sit in plain sight. The cards below cover the gaps that tend to remain after a policy is written, and the control and check that close each one.

Expiry dates missed or extended silently

A VALID UNTIL date set on a role expires the password, but nothing alerts anyone before it lapses, and someone with the right privilege can push the date out with a single ALTER ROLE statement. Set expiry as part of the provisioning script, and review changes to rolvaliduntil in pg_authid as part of a regular access review. Check that expiry dates exist for every login role that is not a directory-managed account, and that extensions to those dates have an owner and a ticket.

Password reuse across environments

Teams often copy the same database password from development into staging and production, or hand one password to several services. A leak from a low-trust environment then opens production. Generate a separate password per cluster and per role, store it in a secrets manager, and confirm that no two environments share a role password by testing a rotated value against each one.

Superusers who bypass policy

A role with superuser rights can change any other role's password, its expiry date and its login attribute, so a policy applied to ordinary roles does not bind the people who hold those rights. Limit superuser to a small break-glass set of roles, keep their passwords outside routine use, and check pg_roles for every role with rolsuper set. Any change to those attributes should appear in the server log.

Passwords exposed in logs and scripts

Passwords reach logs through CREATE ROLE ... PASSWORD and ALTER ROLE statements, shell history, connection strings in cron jobs and application config files. Use \password in psql so the client hashes the value before it is sent, and keep statement logging limited so that password text is not captured. Search configuration repositories, CI logs and the server log for the literal word PASSWORD followed by a value.

Long-lived service account credentials

Application and batch accounts often hold a password that never rotates because rotation would break a running service. These accounts are the most likely target for an attacker who obtains a connection string, and they are rarely covered by an expiry date because the expiry would stop the service. Give each service its own role, rotate its password on a schedule through a secrets manager, and check that rotation has been tested against the application before the old password is removed.

Lockout that exists only in a directory

When a directory such as Active Directory locks accounts after repeated failures, that lock does not protect a PostgreSQL role authenticated with a local password, because the database does not consult the directory. An attacker can keep guessing against the database port while the directory stays clean. Confirm which roles authenticate through LDAP or another external method, restrict password-based access with pg_hba.conf, and verify that a failed-login test against a local role is caught by monitoring you control.

Authentication methods

How to push password policy to a directory or SSO

The safest way to get complexity, expiry and lockout rules for PostgreSQL logins is to stop storing those passwords in PostgreSQL at all and let a directory or identity provider own the credential. PostgreSQL's LDAP method works in two patterns. In simple bind, the server builds a bind name from the username and tries it directly against the directory. In search+bind, the server first searches the directory for the user's entry, then binds as that entry. Search+bind is usually the better fit because the user's distinguished name does not have to match a fixed pattern. Either way, the roles must still exist inside PostgreSQL, because the directory only answers whether the password is correct. Directory policy such as complexity, expiry or account lockout applies only to logins that actually pass through that directory, so any role left on a local password method keeps the database's own rules, which is usually none.

PostgreSQL 18 adds an OAuth 2.0 method, which moves the credential question to the identity provider. The database receives a token, and a validator library checks it, so password rules, multi-factor prompts and lockout are enforced where users sign in. Planning this path has concrete work: you must choose or build a validator that suits your token format, match the expected issuer exactly, decide which scopes grant access, and map token identities to PostgreSQL roles. Confirm the validator and mapping details against the PostgreSQL authentication methods documentation and your identity provider's token contents before you commit to a design. Service accounts and automated jobs need a separate route, such as certificates or a local role with a managed secret.

Any directory-backed or token-backed design makes login depend on an outside service. When the directory or identity provider is unreachable, users and applications that rely on it cannot connect, and existing sessions may be the only access left. Decide in advance which accounts stay reachable during an outage, such as a small set of named break-glass roles that authenticate through a separate method, with credentials held in a sealed store, rotated after each use and monitored for logins. Test the outage path before you need it by stopping the directory in a non-production environment and confirming that break-glass access works and that alerts fire. Document who may invoke it, how the event is reviewed, and how normal authentication is restored.

Limits to confirm in your environment before you rely on this

PostgreSQL leaves much of password policy and lockout to the surrounding stack, so the controls you can rely on depend on your major version, client tooling, operating system and identity provider. The items below are the points to verify with the vendor documentation and your own test results before you write a policy or tell an auditor that a control exists.

Passwordcheck behavior for your version

The passwordcheck module is the only built-in check on password content, and it is loaded through shared_preload_libraries. Confirm exactly which rules it applies in your major version, whether it rejects passwords that match the role name or are too short, and how it behaves for ALTER ROLE as well as CREATE ROLE. Test a set of deliberately weak passwords on a staging cluster, because the module checks only what it is designed to check and does not measure password strength.

Authentication docs

Driver and pooler support for the auth method

Your password policy only protects you if every client can authenticate with the method you chose. Confirm that your application drivers, ORMs, migration tools and connection poolers support SCRAM-SHA-256 and the scram-sha-256 method in pg_hba.conf, and that they handle password expiry errors. Poolers such as PgBouncer have their own authentication configuration, so test how they pass credentials and whether a password change on the server reaches pooled connections.

PAM and operating system lockout

If you route database logins through PAM, lockout may come from the operating system rather than from PostgreSQL, and its scope may differ from what you expect. Confirm which PAM stack applies to the database service, whether its failure counter is per user or per source address, and whether it resets automatically or needs an administrator. Test a lockout end to end, including the effect on replication and monitoring accounts, which often run without interactive use.

Directory or SSO enforcement for database logins

When a directory or SSO provider holds the policy, check whether it applies to database logins at all. An LDAP or Active Directory bind may succeed for a disabled account only if the directory is not consulted, and a password expiry in the directory may not be enforced by the database. Confirm which lockout, expiry and complexity settings apply to the bind account, how a locked or disabled user is reflected in PostgreSQL roles, and what happens when the directory is unreachable.

Failed login capture in audit logs

Failed authentication attempts appear in the server log only if logging settings capture them, and the detail depends on your configuration. Confirm which settings record connection failures and authentication messages, whether the log includes the client address and role name, and whether your log pipeline keeps those fields. Verify that a lockout alert can be built from the events you actually collect, and check how long those logs are retained.

Specific control requirements in your framework

Your compliance framework may require a particular account lockout threshold, password history or expiry interval, and PostgreSQL will not provide those settings on its own. Identify the control IDs that apply to your environment, map each one to the component that enforces it (PostgreSQL, the operating system, the directory or the application), and agree the evidence with your assessor before you build the configuration. Do not assume that a passing staging test satisfies the control.

What teams ask before they set PostgreSQL password policy

Can PostgreSQL lock an account after repeated failed logins?

No. PostgreSQL does not have a built-in account lockout feature that disables a role after a number of failed password attempts. Failed logins are recorded in the server log when logging is configured to capture them, but the server itself does not count them or block the account. If lockout is a requirement, teams usually choose between an external control such as a directory or SSO provider that enforces lockout before the password reaches PostgreSQL, a fail-count mechanism built with an extension or custom tooling, or a compensating control such as log monitoring with a documented response. Ask the vendor to confirm which option fits your authentication method, since a password check that happens inside the database behaves differently from one performed by a directory. Your written scope should name the chosen control and how it is tested.

How do we enforce password complexity in PostgreSQL?

The bundled passwordcheck module can reject passwords that are too short or that match the role name, and it can be extended with custom checks. It runs only when a password is set through SQL commands such as CREATE ROLE or ALTER ROLE, so it does not validate passwords held by an external directory. Two questions shape the work: where passwords are actually created, and whether your standard requires rules that passwordcheck cannot express, such as history or dictionary checks. Confirm the module is available in your packaged version, test that it rejects weak passwords on every path your administrators use, and decide whether the rule belongs in the database or in the identity system that owns the user.

Does VALID UNTIL give us password rotation?

Only partly. VALID UNTIL sets a date after which a role can no longer log in with a password, which enforces expiry but does not force a user to choose a new password, track history or notify anyone before the date passes. Expiry also affects only password-based login, so roles that authenticate by certificate, Kerberos, LDAP or another method need their own review. Expired roles can lock out production services if no one plans the renewal, so many teams pair the setting with a tracked owner, an alert before the date, and a tested rotation runbook. Ask for a written list of which roles use passwords, which use other methods, and who renews each one.

What drives the cost and effort of a password policy project?

The main drivers are the number of authentication methods in use, the number of roles and services that hold passwords, how mature your directory or SSO is, and how strict your change control must be. A deployment with one password method and a small set of roles is far simpler than one that mixes LDAP, certificates and application-held credentials across many clusters. Costs are split between the work of mapping current roles and methods, designing the policy and any custom checks, testing on non-production clusters, and handing over runbooks. Ongoing costs come from reviewing expiry, rotating credentials and retesting after upgrades. DeployOpen does not publish prices. Request a quote built from a written scope that lists the clusters, roles, methods and deliverables, with the effort for each one shown separately.

What do we need to provide for the engagement to go smoothly?

Expect to provide an inventory of clusters and their versions, the current pg_hba.conf rules and role list, the identity system that owns your users, and the password standard your auditors or security team apply. You also need a named contact who can approve changes to production roles and a window for testing. Access to a non-production environment that mirrors the authentication setup is important, because password changes can break services in ways that only appear under real login traffic. Your team keeps ownership of the identity provider and of any service credentials that the work rotates.

Can our own team set up password policy without outside help?

Yes, if you have database administrators who can test changes safely and an identity team that can confirm where policy should live. Setting VALID UNTIL, loading passwordcheck and reviewing pg_hba.conf are documented tasks that a competent team can complete. The harder parts are deciding whether lockout belongs in the directory, designing a rotation process that does not break services, and proving the controls work for an assessor. An outside partner is most useful when several methods, many clusters or a strict compliance deadline are involved. Whichever route you choose, ask for the result in writing: the policy, the test evidence and the runbook.

How DeployOpen scopes and delivers PostgreSQL password controls

DeployOpen plans and builds PostgreSQL password controls inside infrastructure you control, starting from what PostgreSQL enforces natively and what must come from a directory, SSO provider or monitoring layer. Engagements are fixed-scope, with the policy decisions, configuration and evidence agreed in writing before work starts.

How we scope

  • Inventory every PostgreSQL cluster, role type and login path that accepts password authentication today.
  • Record which accounts are human, service or break-glass, and who owns each password lifecycle.
  • Agree in writing which controls PostgreSQL handles directly and which move to a directory or SSO provider.

How we build

  • Configure the passwordcheck module, SCRAM password storage and VALID UNTIL settings where the chosen design uses them.
  • Build failed-login monitoring from server logs, with alerting and a documented response, since core PostgreSQL does not lock accounts.
  • Connect password-based login to a directory or SSO service where policy and lockout are enforced centrally, and test each path.

How we hand over

  • Test password rules, expiry behavior, failed-login alerts and lockout responses against the agreed scope before handover.
  • Document the policy, configuration files, role ownership and the runbook for unlocking or resetting accounts.
  • Deliver a written record of what each control covers and what remains with your team or auditors to assess.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers for password controls after handover.
  • Review policy changes when PostgreSQL releases or your directory and SSO configuration change.
  • Confirm the supported PostgreSQL version and password method with the vendor before each planned change.

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.