Skip to main content

CreatePostgresDbIfNotExists

Creates a PostgreSQL database if it does not already exist, using the provided configuration. If the database already exists, it connects to it; otherwise, it connects to the default 'postgres' database to create the specified database before establishing a connection to the newly created database.

def CreatePostgresDbIfNotExists(
ctx: context.Context,
pgConfig: PostgresConfig
) - > *sqlx.DB, error

Creates a PostgreSQL database if it does not already exist, using the provided configuration. This function ensures that the specified database is available for use, connecting to a default 'postgres' database first if the target database does not exist to perform the creation.

Parameters

NameTypeDescription
ctxcontext.ContextThe context for the operation, allowing for cancellation and timeouts.
pgConfigPostgresConfigThe configuration details for the PostgreSQL database, including connection parameters like host, port, user, password, and the target database name.

Returns

TypeDescription
*sqlx.DB, errorA pointer to an sqlx.DB object representing the connection to the PostgreSQL database, or an error if the connection or creation fails.