postgres

package module
v0.0.0-...-165edf6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithConfigFile

func WithConfigFile(cfg string) testcontainers.CustomizeRequestOption

WithConfigFile sets the config file to be used for the postgres container It will also set the "config_file" parameter to the path of the config file as a command line argument to the container

func WithDatabase

func WithDatabase(dbName string) testcontainers.CustomizeRequestOption

WithDatabase sets the initial database to be created when the container starts It can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of WithUser will be used.

func WithInitScripts

func WithInitScripts(scripts ...string) testcontainers.CustomizeRequestOption

WithInitScripts sets the init scripts to be run when the container starts

func WithPassword

func WithPassword(password string) testcontainers.CustomizeRequestOption

WithPassword sets the initial password of the user to be created when the container starts It is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL.

func WithUsername

func WithUsername(user string) testcontainers.CustomizeRequestOption

WithUsername sets the initial username to be created when the container starts It is used in conjunction with WithPassword to set a user and its password. It will create the specified user with superuser power and a database with the same name. If it is not specified, then the default user of postgres will be used.

Types

type PostgresContainer

type PostgresContainer struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

PostgresContainer represents the postgres container type used in the module

func RunContainer

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*PostgresContainer, error)

RunContainer creates an instance of the postgres container type

Example
// runPostgresContainer {
ctx := context.Background()

dbName := "users"
dbUser := "user"
dbPassword := "password"

postgresContainer, err := postgres.RunContainer(ctx,
	testcontainers.WithImage("docker.io/postgres:15.2-alpine"),
	postgres.WithInitScripts(filepath.Join("testdata", "init-user-db.sh")),
	postgres.WithConfigFile(filepath.Join("testdata", "my-postgres.conf")),
	postgres.WithDatabase(dbName),
	postgres.WithUsername(dbUser),
	postgres.WithPassword(dbPassword),
	testcontainers.WithWaitStrategy(
		wait.ForLog("database system is ready to accept connections").
			WithOccurrence(2).
			WithStartupTimeout(5*time.Second)),
)
if err != nil {
	panic(err)
}

// Clean up the container
defer func() {
	if err := postgresContainer.Terminate(ctx); err != nil {
		panic(err)
	}
}()
// }

state, err := postgresContainer.State(ctx)
if err != nil {
	panic(err)
}

fmt.Println(state.Running)
Output:

true

func RunContainer2

func RunContainer2(ctx context.Context, dirShare map[string]string, opts ...testcontainers.ContainerCustomizer) (*PostgresContainer, error)

func (*PostgresContainer) ConnectionString

func (c *PostgresContainer) ConnectionString(ctx context.Context, args ...string) (string, error)

ConnectionString returns the connection string for the postgres container, using the default 5432 port, and obtaining the host and exposed port from the container. It also accepts a variadic list of extra arguments which will be appended to the connection string. The format of the extra arguments is the same as the connection string format, e.g. "connect_timeout=10" or "application_name=myapp"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL