jackc/pgx · error

failed to read service file

Error message

failed to read service file: %v

What it means

The pg_service.conf file referenced by the connection string could not be read or parsed. The message shows the attempted service file path (note: the format verb is %v applied to the path, the underlying read error is not chained).

Solutions

  1. Verify the service file path exists and is readable
  2. Check the service file syntax matches pg_service.conf format
  3. Inspect file permissions
  4. Point PGSERVICEFILE to the correct location or fix the service entry
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgconn/config.go:788 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/ef3b292201c6720a. Report an issue: GitHub.

Appendix: source

Thrown at pgconn/config.go:788

		key = canonicalConnStringKey(key)

		if key == "" {
			return nil, errors.New("invalid keyword/value")
		}

		if key == "user" && val == "" {
			continue
		}
		settings[key] = val
	}

	return settings, nil
}

func parseServiceSettings(servicefilePath, serviceName string) (map[string]string, error) {
	servicefile, err := pgservicefile.ReadServicefile(servicefilePath)
	if err != nil {
		return nil, fmt.Errorf("failed to read service file: %v", servicefilePath)
	}

	service, err := servicefile.GetService(serviceName)
	if err != nil {
		return nil, fmt.Errorf("unable to find service: %v", serviceName)
	}

	settings := make(map[string]string, len(service.Settings))
	for k, v := range service.Settings {
		settings[canonicalConnStringKey(k)] = v
	}

	return settings, nil
}

// configTLS uses libpq's TLS parameters to construct  []*tls.Config. It is
// necessary to allow returning multiple TLS configs as sslmode "allow" and
// "prefer" allow fallback.

View on GitHub (pinned to ec1a0befd2)