@@ -27,8 +27,8 @@ import (
|
|||||||
type AfterConnectFunc func(ctx context.Context, pgconn *PgConn) error
|
type AfterConnectFunc func(ctx context.Context, pgconn *PgConn) error
|
||||||
type ValidateConnectFunc func(ctx context.Context, pgconn *PgConn) error
|
type ValidateConnectFunc func(ctx context.Context, pgconn *PgConn) error
|
||||||
|
|
||||||
// Config is the settings used to establish a connection to a PostgreSQL server. It must be created by ParseConfig and
|
// Config is the settings used to establish a connection to a PostgreSQL server. It must be created by ParseConfig. A
|
||||||
// then it can be modified. A manually initialized Config will cause ConnectConfig to panic.
|
// manually initialized Config will cause ConnectConfig to panic.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string // host (e.g. localhost) or absolute path to unix domain socket directory (e.g. /private/tmp)
|
Host string // host (e.g. localhost) or absolute path to unix domain socket directory (e.g. /private/tmp)
|
||||||
Port uint16
|
Port uint16
|
||||||
@@ -112,18 +112,6 @@ func NetworkAddress(host string, port uint16) (network, address string) {
|
|||||||
return network, address
|
return network, address
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig returns an *Config without parsing a connection string or reading the standard PG* environment variables.
|
|
||||||
// Host, Port, Database, User, and Password must be set before the config can be used to establish a connection.
|
|
||||||
func NewConfig() *Config {
|
|
||||||
return &Config{
|
|
||||||
DialFunc: makeDefaultDialer().DialContext,
|
|
||||||
LookupFunc: makeDefaultResolver().LookupHost,
|
|
||||||
BuildFrontend: makeDefaultBuildFrontendFunc(8192),
|
|
||||||
RuntimeParams: map[string]string{},
|
|
||||||
createdByParseConfig: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseConfig builds a *Config with similar behavior to the PostgreSQL standard C library libpq. It uses the same
|
// ParseConfig builds a *Config with similar behavior to the PostgreSQL standard C library libpq. It uses the same
|
||||||
// defaults as libpq (e.g. port=5432) and understands most PG* environment variables. connString may be a URL or a DSN.
|
// defaults as libpq (e.g. port=5432) and understands most PG* environment variables. connString may be a URL or a DSN.
|
||||||
// It also may be empty to only read from the environment. If a password is not supplied it will attempt to read the
|
// It also may be empty to only read from the environment. If a password is not supplied it will attempt to read the
|
||||||
@@ -135,6 +123,11 @@ func NewConfig() *Config {
|
|||||||
// # Example URL
|
// # Example URL
|
||||||
// postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca
|
// postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca
|
||||||
//
|
//
|
||||||
|
// The returned *Config may be modified. However, it is strongly recommended that any configuration that can be done
|
||||||
|
// through the connection string be done there. In particular the fields Host, Port, TLSConfig, and Fallbacks can be
|
||||||
|
// interdependent (e.g. TLSConfig needs knowledge of the host to validate the server certificate). These fields should
|
||||||
|
// not be modified individually. They should all be modified or all left unchanged.
|
||||||
|
//
|
||||||
// ParseConfig supports specifying multiple hosts in similar manner to libpq. Host and port may include comma separated
|
// ParseConfig supports specifying multiple hosts in similar manner to libpq. Host and port may include comma separated
|
||||||
// values that will be tried in order. This can be used as part of a high availability system. See
|
// values that will be tried in order. This can be used as part of a high availability system. See
|
||||||
// https://www.postgresql.org/docs/11/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS for more information.
|
// https://www.postgresql.org/docs/11/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS for more information.
|
||||||
@@ -175,11 +168,11 @@ func NewConfig() *Config {
|
|||||||
// security each sslmode provides.
|
// security each sslmode provides.
|
||||||
//
|
//
|
||||||
// The sslmode "prefer" (the default), sslmode "allow", and multiple hosts are implemented via the Fallbacks field of
|
// The sslmode "prefer" (the default), sslmode "allow", and multiple hosts are implemented via the Fallbacks field of
|
||||||
// the Config struct. If the main TLS config is manually changed it will not affect the fallbacks. For example, in the
|
// the Config struct. If TLSConfig is manually changed it will not affect the fallbacks. For example, in the case of
|
||||||
// case of sslmode "prefer" this means it will first try the main Config settings which use TLS, then it will try
|
// sslmode "prefer" this means it will first try the main Config settings which use TLS, then it will try the fallback
|
||||||
// the fallback which does not use TLS. This can lead to an unexpected unencrypted connection if the main TLS config
|
// which does not use TLS. This can lead to an unexpected unencrypted connection if the main TLS config is manually
|
||||||
// is manually changed later but the unencrypted fallback is present. Remove or update all fallbacks or use NewConfig
|
// changed later but the unencrypted fallback is present. Ensure there are no stale fallbacks when manually setting
|
||||||
// to build the config manually.
|
// TLCConfig.
|
||||||
//
|
//
|
||||||
// Other known differences with libpq:
|
// Other known differences with libpq:
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -198,24 +198,6 @@ func TestConnectWithConnectionRefused(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectConfigFromNewConfig(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
baseConfig, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_CONN_STRING"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
config := pgconn.NewConfig()
|
|
||||||
config.Host = baseConfig.Host
|
|
||||||
config.Port = baseConfig.Port
|
|
||||||
config.Database = baseConfig.Database
|
|
||||||
config.User = baseConfig.User
|
|
||||||
config.Password = baseConfig.Password
|
|
||||||
|
|
||||||
conn, err := pgconn.ConnectConfig(context.Background(), config)
|
|
||||||
require.NoError(t, err)
|
|
||||||
closeConn(t, conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConnectCustomDialer(t *testing.T) {
|
func TestConnectCustomDialer(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user