Safeguard
Security Guides•

Can a PostgreSQL role password expire? Using VALID UNTIL

PostgreSQL's VALID UNTIL clause sets a date after which a role's password stops working. Use it for contractors, and automate rotation for services.

Safeguard Security Team
4 min read

Yes. PostgreSQL's VALID UNTIL clause sets a date and time after which a role's password is no longer valid. If you omit it, the password is valid for all time. It suits temporary access for contractors or break glass accounts, but service accounts need automated rotation first or they will fail on the expiry date.

What the docs say

The CREATE ROLE reference says the VALID UNTIL clause sets a date and time after which the role's password is no longer valid, and that if the clause is omitted the password will be valid for all time. The ALTER ROLE page shows examples: setting a specific expiry with a timestamp, making a password valid forever with the value infinity, and removing a password entirely with PASSWORD NULL.

ALTER ROLE contractor_amy VALID UNTIL '2026-12-31 23:59:59+00';
ALTER ROLE svc_reports VALID UNTIL 'infinity';
ALTER ROLE old_service PASSWORD NULL;

Where it fits

Role typeGood use of VALID UNTILWatch out for
Contractor or temporary humanYes, set an end date at onboardingRemember to extend if the work continues
Break glass accountShort expiry, renewed after each testTest before the real emergency
Application service accountOnly with automated rotationA forgotten expiry causes an outage
Read only analystYes, with periodic reviewShared credentials defeat the purpose

The documented behavior is about the password. If a role authenticates by another method, think about what controls its end date there. For example, certificates have their own expiry, and cloud identity tokens are short lived by design. See IAM tokens instead of a database password.

The outage risk

An expiring password on a service account is a scheduled failure unless something changes it first. The login stops working at the stated time, and the first sign is connection errors in production. If you use expiry on service accounts:

  1. Automate the change of the password and the update of the stored secret together.
  2. Alert well before the expiry date, with a date you can see in a query rather than in someone's memory.
  3. Test the rotation path in staging with the same pool and driver settings.
  4. Remember that existing connections are not necessarily dropped at expiry, while new ones fail. Check how your pool reconnects.

Point 4 is a behavior to test, not assume, because it depends on your setup.

An access review that uses it

  1. List roles that can log in.
  2. For each, record the owner, purpose and whether the password has an end date.
  3. For human and contractor accounts, add an expiry that matches the contract.
  4. For service accounts, decide between automated rotation and a short lived token approach.
  5. Remove roles that nobody can name an owner for. The OWASP guidance says to remove accounts when an application is decommissioned and to review accounts regularly.
  6. Re-run the review on a calendar. See what OWASP says about securing a backend database.

Pair it with least privilege

An expiry limits how long a credential lives. Privileges limit what it can do while alive. Use both. See PostgreSQL predefined roles and least privilege and statement timeouts and connection limits per role.

Safeguard scans dependencies, including database drivers, and can find exposed credentials in repositories. A free scan is at gold.safeguard.sh/scan.

Quick check

What happens if I omit VALID UNTIL?

The CREATE ROLE docs say the password will be valid for all time.

How do I remove a password?

ALTER ROLE with PASSWORD NULL, as shown in the ALTER ROLE examples.

Should service accounts expire?

Only with automated rotation in place, or the expiry becomes a scheduled outage.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.