$ show lib/pq
pq is a pure Go PostgreSQL driver for the database/sql interface. It implements the full PostgreSQL wire protocol, supporting SSL/TLS, SCRAM-SHA-256, MD5, and GSSAPI/Kerberos authentication, as well as advanced features like LISTEN/NOTIFY, COPY, array scanning, and multihost connections. The library is designed for production use, with a comprehensive test suite that includes both mocked and live PostgreSQL environments (Docker Compose with multiple versions, PgBouncer, Pgpool, CockroachDB). Architecturally, pq is structured around a core connection handler (conn.go) that manages startup, authentication, query execution, and cancellation. Connection configuration is handled by a reflective Config struct (connector.go) that maps DSN keys and environment variables to struct fields. Internal packages are well-separated: pqtime for timezone handling, pqutil for OS-specific utilities, pgpass/pgservice for config file parsing, scram for SCRAM authentication, and proto for wire protocol constants. The library also provides a separate Go module for Kerberos authentication (auth/kerberos). Maintenance is active, with recent releases (v1.12.3 in 2026) and ongoing issue triage. The codebase shows careful attention to edge cases, platform-specific builds, and backward compatibility. While some deprecated items exist (e.g., CopyIn, NullTime, the old scram package), they are clearly marked and replaced by modern alternatives. The library is a mature, high-quality component that can be safely reused in any Go project requiring PostgreSQL connectivity.
The LLM identified 5 files worth highlighting for reuse:
Provides timezone-aware timestamp parsing and formatting for PostgreSQL. Uses a cached location map to avoid repeated I/O, and handles edge cases like infinity timestamps. The pattern of caching expensive lookups and providing a clean API for date/time conversion is reusable in any database driver or data processing pipeline.
Implements the SCRAM-SHA-256 client authentication protocol as specified by RFC 5802. Handles nonce generation, salted password derivation (via PBKDF2), and message exchange. The code is self-contained, well-structured, and could be extracted for use in other database drivers or authentication libraries that need SCRAM support.
Defines the Config struct and its reflective mapping from DSN keys and environment variables to struct fields via setFromTag(). This pattern allows flexible, extensible configuration without boilerplate. The tomap() and string() methods provide serialization for testing and debugging. The approach is reusable for any library that needs to parse connection strings or configuration maps into a typed struct.
Defines the pq.Error type with Severity, Code, Message, and Detail fields, plus an As() function for matching error codes. This pattern provides structured error information from PostgreSQL and allows callers to handle specific error conditions (e.g., unique violation) without parsing strings. The design is reusable for any system that needs to expose rich, typed error information from a protocol.
Implements GenericArray and ArrayDelimiter interface for scanning and valuing PostgreSQL array columns into Go slices. Uses reflection to handle arbitrary element types, with support for custom delimiters. The pattern of providing a generic, reusable array handler that works with database/sql is applicable to any SQL driver that needs to handle array types.