Skip to content

postgres: NewPool checks the server certificate against "localhost" and keeps localhost fallbacks #492

Closed

Description

@peppescg

Bug

buildPoolConfig in postgres/pool.go (line 135 on main, also in v0.0.59) parses a bootstrap DSN for localhost and only then assigns the real target:

bootDSN := "postgres://localhost?sslmode=" + url.QueryEscape(sslMode)
pc, err := pgxpool.ParseConfig(bootDSN)
...
pc.ConnConfig.Host = cfg.Host
pc.ConnConfig.Port = uint16(cfg.Port)

pgxpool.ParseConfig builds the TLS settings and the fallback list for the host it parses, localhost. Reassigning ConnConfig.Host afterwards does not update them:

  • sslMode: verify-full never connects to a real server. pgx sets TLSConfig.ServerName to localhost, so the server certificate is checked against localhost instead of cfg.Host, and the hostname check fails.
  • sslMode: prefer and allow keep a fallback to localhost:5432. If the first attempt fails, pgx tries localhost:5432 instead of cfg.Host; with prefer that attempt is unencrypted.

require (the default) and verify-ca are not affected: neither checks the host name and neither has a fallback.

Fix

In buildPoolConfig, after assigning Host and Port:

  • when pc.ConnConfig.TLSConfig != nil, set pc.ConnConfig.TLSConfig.ServerName = cfg.Host;
  • point every entry of pc.ConnConfig.Fallbacks at cfg.Host / cfg.Port (setting ServerName on each fallback's TLSConfig too), or clear Fallbacks for the modes that have none.

Test

A unit test over each sslMode can assert, without a server, that ServerName equals cfg.Host and that no fallback targets localhost. An integration test against a Postgres serving a certificate for a non-localhost name would cover verify-full end to end.

Activity

  1. added
    needs-triage
    on Nov 10, 2026
  2. self-assigned this
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

needs-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions