postgres

package module
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 6 Imported by: 52

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 {
	log.Fatalf("failed to start container: %s", err)
}

// Clean up the container
defer func() {
	if err := postgresContainer.Terminate(ctx); err != nil {
		log.Fatalf("failed to terminate container: %s", err)
	}
}()
// }

state, err := postgresContainer.State(ctx)
if err != nil {
	log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)
Output:

true

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"

func (*PostgresContainer) MustConnectionString added in v0.30.0

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

MustConnectionString panics if the address cannot be determined.

func (*PostgresContainer) Restore added in v0.28.0

func (c *PostgresContainer) Restore(ctx context.Context, opts ...SnapshotOption) error

Restore will restore the database to a specific snapshot. By default, it will restore the last snapshot taken on the database by the Snapshot method. If a snapshot name is provided, it will instead try to restore the snapshot by name.

func (*PostgresContainer) Snapshot added in v0.28.0

func (c *PostgresContainer) Snapshot(ctx context.Context, opts ...SnapshotOption) error

Snapshot takes a snapshot of the current state of the database as a template, which can then be restored using the Restore method. By default, the snapshot will be created under a database called migrated_template, you can customize the snapshot name with the options. If a snapshot already exists under the given/default name, it will be overwritten with the new snapshot.

type SnapshotOption added in v0.28.0

type SnapshotOption func(container *snapshotConfig) *snapshotConfig

SnapshotOption is the type for passing options to the snapshot function of the database

func WithSnapshotName added in v0.28.0

func WithSnapshotName(name string) SnapshotOption

WithSnapshotName adds a specific name to the snapshot database created from the main database defined on the container. The snapshot must not have the same name as your main database, otherwise it will be overwritten

Jump to

Keyboard shortcuts

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