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 one.

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 to perform the creation if necessary.

Parameters

NameTypeDescription
ctxcontext.ContextThe context for the operation, allowing for cancellation and timeouts.
pgConfigPostgresConfigThe configuration details for the PostgreSQL database, including connection parameters and the database name to be created or connected to.

Returns

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