MCPcopy
hub / github.com/jackc/pgx / ConnectConfig

Function ConnectConfig

pgconn/pgconn.go:142–175  ·  view source on GitHub ↗

Connect establishes a connection to a PostgreSQL server using config. config must have been constructed with [ParseConfig]. ctx can be used to cancel a connect attempt. If config.Fallbacks are present they will sequentially be tried in case of error establishing network connection. An authenticatio

(ctx context.Context, config *Config)

Source from the content-addressed store, hash-verified

140// authentication error will terminate the chain of attempts (like libpq:
141// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS) and be returned as the error.
142func ConnectConfig(ctx context.Context, config *Config) (*PgConn, error) {
143 // Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from
144 // zero values.
145 if !config.createdByParseConfig {
146 panic("config must be created by ParseConfig")
147 }
148
149 var allErrors []error
150
151 connectConfigs, errs := buildConnectOneConfigs(ctx, config)
152 if len(errs) > 0 {
153 allErrors = append(allErrors, errs...)
154 }
155
156 if len(connectConfigs) == 0 {
157 return nil, &ConnectError{Config: config, err: fmt.Errorf("hostname resolving error: %w", errors.Join(allErrors...))}
158 }
159
160 pgConn, errs := connectPreferred(ctx, config, connectConfigs)
161 if len(errs) > 0 {
162 allErrors = append(allErrors, errs...)
163 return nil, &ConnectError{Config: config, err: errors.Join(allErrors...)}
164 }
165
166 if config.AfterConnect != nil {
167 err := config.AfterConnect(ctx, pgConn)
168 if err != nil {
169 pgConn.conn.Close()
170 return nil, &ConnectError{Config: config, err: fmt.Errorf("AfterConnect error: %w", err)}
171 }
172 }
173
174 return pgConn, nil
175}
176
177// buildConnectOneConfigs resolves hostnames and builds a list of connectOneConfigs to try connecting to. It returns a
178// slice of successfully resolved connectOneConfigs and a slice of errors. It is possible for both slices to contain

Calls 3

buildConnectOneConfigsFunction · 0.85
connectPreferredFunction · 0.85
CloseMethod · 0.65