{"record":{"id":"37d45bf948dbd3c6","repo":"getsops/sops","slug":"pinging-audit-database-failed-s","errorCode":null,"errorMessage":"Pinging audit database failed: %s","messagePattern":"Pinging audit database failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"audit/audit.go","lineNumber":127,"sourceCode":"// It persists the audit event by writing a row to the 'audit_event' table.\n// Errors with writing to the database will output a log message and the\n// process will exit with status set to 1\ntype PostgresAuditor struct {\n\tDB *sql.DB\n}\n\n// NewPostgresAuditor is the constructor for a new PostgresAuditor struct\n// initialized with the given db connection string\nfunc NewPostgresAuditor(connStr string) (*PostgresAuditor, error) {\n\tdb, err := sql.Open(\"postgres\", connStr)\n\tpg := &PostgresAuditor{DB: db}\n\tif err != nil {\n\t\treturn pg, err\n\t}\n\tvar result int\n\terr = pg.DB.QueryRow(\"SELECT 1\").Scan(&result)\n\tif err != nil {\n\t\treturn pg, fmt.Errorf(\"Pinging audit database failed: %s\", err)\n\t} else if result != 1 {\n\t\treturn pg, fmt.Errorf(\"Database malfunction: SELECT 1 should return 1, but returned %d\", result)\n\t}\n\treturn pg, nil\n}\n\n// Handle persists the audit event by writing a row to the\n// 'audit_event' postgres table\nfunc (p *PostgresAuditor) Handle(event interface{}) {\n\tu, err := user.Current()\n\tif err != nil {\n\t\tlog.Fatalf(\"Error getting current user for auditing: %s\", err)\n\t}\n\tswitch event := event.(type) {\n\tcase DecryptEvent:\n\t\t// Save the event to the database\n\t\tlog.WithField(\"file\", event.File).\n\t\t\tDebug(\"Saving decrypt event to database\")","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/getsops/sops/blob/13442bb98183887d7a9ac09ec8ab0564673a59d8/audit/audit.go#L109-L145","documentation":"NewPostgresAuditor verifies connectivity to the audit database by running SELECT 1 before returning the auditor. This error means the QueryRow(\"SELECT 1\").Scan failed, i.e. the database is unreachable, credentials are wrong, or the connection dropped.","triggerScenarios":"init calls NewPostgresAuditor; pg.DB.QueryRow(\"SELECT 1\").Scan returns a driver-level error: connection refused, authentication failure, TLS mismatch, DNS failure, or context timeout.","commonSituations":"Postgres not running or wrong host/port in config; bad username/password; database doesn't exist; pg_hba.conf rejecting the connection; network/firewall blocking; SSL mode mismatch.","solutions":["Confirm Postgres is reachable: psql \"<same DSN>\" -c 'SELECT 1'","Check the connection string (host, port, user, password, dbname) in the audit config/environment","Inspect server logs and pg_hba.conf to confirm the client host/auth method is allowed","Verify network path: firewall, security groups, VPN, DNS resolution for the DB host"],"exampleFix":"// before\nNewPostgresAuditor(\"postgres://user:pass@localhost:5433/auditdb\")  # wrong port\n// after\nNewPostgresAuditor(\"postgres://user:pass@localhost:5432/auditdb\")","handlingStrategy":"retry","validationCode":"conn := fmt.Sprintf(\"postgres://%s:%s@%s:%s/%s?sslmode=require\", user, pass, host, port, db)\ndb, err := sql.Open(\"postgres\", conn)\nif err != nil { return err }\nctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\ndefer cancel()\nif err := db.PingContext(ctx); err != nil {\n    return fmt.Errorf(\"audit DB preflight failed: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"pg, err := audit.NewPostgresAuditor(dsn)\nif err != nil {\n    if strings.Contains(err.Error(), \"Pinging audit database failed\") {\n        // retry with backoff or fail fast with a clear ops message\n    }\n    return err\n}","preventionTips":["Preflight with psql/DB Ping using the same DSN","Use health checks/readiness probes for the DB host","Keep DSNs in one validated config source","Monitor DB reachability from the app network"],"tags":["postgres","database","connectivity","audit"],"backgroundTag":"database-connection-failed","analyzedSha":"13442bb98183887d7a9ac09ec8ab0564673a59d8","analyzedAt":"2026-09-01T03:53:00.447Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}