Skip to content

Error: failed to configure TLS (unable to add CA to cert pool) #2132

@nemirlev

Description

@nemirlev

Describe the bug
When setting sslmode=disable in the connection string, I still encounter an error related to TLS configuration. I am using PostgreSQL 16 with the standard Docker image. The error message is:

"Unable to parse config: cannot parse host=localhost port=5432 dbname=postgres user=postgres password=xxxxx target_session_attrs=read-write sslmode=disable: failed to configure TLS (unable to add CA to cert pool)"

Despite explicitly setting sslmode=disable in the connection string, the error persists.

To Reproduce

package main

import (
	"context"
	"fmt"
	"github.com/jackc/pgx/v5"
	"os"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "postgres"
	password = "postgres"
	dbname   = "postgres"
)

func main() {

	connstring := fmt.Sprintf(
		"host=%s port=%d dbname=%s user=%s password=%s target_session_attrs=read-write sslmode=disable",
		host, port, dbname, user, password)

	connConfig, err := pgx.ParseConfig(connstring)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to parse config: %v\n", err)
		os.Exit(1)
	}

	conn, err := pgx.ConnectConfig(context.Background(), connConfig)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
		os.Exit(1)
	}

	defer conn.Close(context.Background())

	var version string

	err = conn.QueryRow(context.Background(), "select version()").Scan(&version)
	if err != nil {
		fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
		os.Exit(1)
	}

	fmt.Println(version)
}
services:
  postgres:
    image: postgres:16
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    ports:
        - "5432:5432"
    volumes:
      - bd_data:/var/lib/postgresql/data

  redis:
    image: redis:latest
    ports:
      - "6379:6379"
  migrate:
    image: migrate/migrate
    volumes:
      - ./migrations:/migrations
    depends_on:
      - postgres
    command: ["-path", "/migrations", "-database", "postgresql://postgres:postgres@postgres:5432/postgres?sslmode=disable", "up"]
volumes:
    bd_data:
    redis_data:

Please run your example with the race detector enabled. For example, go run -race main.go or go test -race.

go run -race connect.go
Unable to parse config: cannot parse `host=localhost port=5432 dbname=postgres user=postgres password=xxxxx target_session_attrs=read-write sslmode=disable`: failed to configure TLS (unable to add CA to cert pool)
exit status 1

Expected behavior
The connection should be established without attempting to configure TLS when sslmode=disable is set.

Actual behavior
The connection attempt fails with the error: "failed to configure TLS (unable to add CA to cert pool)".

Version

  • Go: go1.23.1 darwin/arm64
  • PostgreSQL: PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
  • pgx: github.com/jackc/pgx/v5 v5.7.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions