Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nemirlev opened this issue Sep 26, 2024 · 3 comments
Open

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

nemirlev opened this issue Sep 26, 2024 · 3 comments
Labels

Comments

@nemirlev
Copy link

nemirlev commented Sep 26, 2024

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
@nemirlev nemirlev added the bug label Sep 26, 2024
@jackc
Copy link
Owner

jackc commented Sep 26, 2024

I ran your example.

jack@glados ~/dev/pgx_issues/pgx-2132 ±master⚡ » go run .
PostgreSQL 16.3 (Homebrew) on aarch64-apple-darwin23.4.0, compiled by Apple clang version 15.0.0 (clang-1500.3.9.4), 64-bit

It works for me. Maybe there is something in the environment?

@nemirlev
Copy link
Author

This is very strange, I have used the library before and had no issues. After your message, I created a separate, new project to definitively avoid any possible side effects, but it still doesn't work.

Снимок экрана 2024-09-29 в 15 21 18

@jackc
Copy link
Owner

jackc commented Sep 29, 2024

I would suggest printing out the environment from within your program and seeing if there is anything unexpected, especially PG* variables.

If that doesn't yield anything, I don't know anything else to try besides stepping through with a debugger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants