Categorygithub.com/kaynetik/embedded-postgres
modulepackage
1.3.3
Repository: https://github.com/kaynetik/embedded-postgres.git
Documentation: pkg.go.dev

# README

Godoc Coverage Status Build Status Go Report Card

embedded-postgres

Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test.

When testing this provides a higher level of confidence than using any in memory alternative. It also requires no other external dependencies outside of the Go build ecosystem.

Heavily inspired by Java projects zonkyio/embedded-postgres and opentable/otj-pg-embedded and reliant on the great work being done by zonkyio/embedded-postgres-binaries in order to fetch precompiled binaries from Maven.

Installation

embedded-postgres uses Go modules and as such can be referenced by release version for use as a library. Use the following to add the latest release to your project.

go get -u github.com/fergusstrange/embedded-postgres

How to use

This library aims to require as little configuration as possible, favouring overridable defaults

ConfigurationDefault Value
Usernamepostgres
Passwordpostgres
Databasepostgres
Version12.1.0
RuntimePath$USER_HOME/.embedded-postgres-go/extracted
Port5432
StartTimeout15 Seconds

A single Postgres instance can be created, started and stopped as follows

postgres := embeddedpostgres.NewDatabase()
err := postgres.Start()

// Do test logic

err := postgres.Stop()

or with created with custom configuration

logger := &bytes.Byffer{}
postgres := NewDatabase(DefaultConfig().
            Username("beer").
            Password("wine").
            Database("gin").
            Version(V12).
            RuntimePath("/tmp").
            Port(9876).
            StartTimeout(45 * time.Second).
            Logger(logger))
err := postgres.Start()

// Do test logic

err := postgres.Stop()

It should be noted that if postgres.Stop() is not called then the child Postgres process will not be released and the caller will block.

Examples

There are a number of realistic representations of how to use this library in examples.

Credits

Contributing

View the contributing guide.

# Packages

No description provided by the author

# Functions

DefaultConfig provides a default set of configuration to be used "as is" or modified using the provided builders.
NewDatabase creates a new EmbeddedPostgres struct that can be used to start and stop a Postgres process.

# Constants

Predefined supported Postgres versions.
Predefined supported Postgres versions.
Predefined supported Postgres versions.
Predefined supported Postgres versions.
Predefined supported Postgres versions.

# Structs

No description provided by the author
Config maintains the runtime configuration for the Postgres process to be created.
EmbeddedPostgres maintains all configuration and runtime functions for maintaining the lifecycle of one Postgres process.

# Type aliases

CacheLocator retrieves the location of the Postgres binary cache returning it to location.
No description provided by the author
PostgresVersion represents the semantic version used to fetch and run the Postgres process.
RemoteFetchStrategy provides a strategy to fetch a Postgres binary so that it is available for use.
VersionStrategy provides a strategy that can be used to determine which version of Postgres should be used based on the operating system, architecture and desired Postgres version.