Categorygithub.com/adrianbrad/psqldocker
modulepackage
1.2.1
Repository: https://github.com/adrianbrad/psqldocker.git
Documentation: pkg.go.dev

# README

adrianbrad psqldocker

🚢 psqldocker GitHub release

powered by ory/dockertest.

GitHub go.mod Go version of a Go module GoDoc reference example

CodeFactor Go Report Card codecov

lint-test grype codeql gitleaks


Go package providing lifecycle management for PostgreSQL Docker instances.

Here is an article expanding on the usage of this package.

Leverage Docker to run unit and integration tests against a real PostgreSQL database.

Usage

Recommended: In a TestXxx function

package foo_test

import (
	"testing"

	"github.com/adrianbrad/psqldocker"
)

func TestXxx(t *testing.T) {
    const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"
	
    c, err := psqldocker.NewContainer(
        "user",
        "password",
        "dbName",
        psqldocker.WithContainerName("test"), 
        // initialize with a schema
        psqldocker.WithSql(schema),
        // you can add other options here
    )
    if err != nil {
        t.Fatalf("cannot start new psql container: %s\n", err)
    }
	
    t.Cleanup(func() {
        err = c.Close()
        if err != nil {
            t.Logf("err while closing conainter: %w", err)
        }
    })
	
    t.Run("Subtest", func(t *testing.T) {
        // execute your test logic here 
    })
}

In a TestMain function

package foo_test

import (
	"log"
	"testing"

	"github.com/adrianbrad/psqldocker"
)

func TestMain(m *testing.M) {
    const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"

    c, err := psqldocker.NewContainer(
        "user",
        "password",
        "dbName",
        psqldocker.WithContainerName("test"), 
        // initialize with a schema
        psqldocker.WithSql(schema),
        // you can add other options here
    )
    if err != nil {
        log.Fatalf("cannot start new psql container: %s\n", err)
    }
	
    defer func() {
        err = c.Close()
        if err != nil {
            log.Printf("err while closing conainter: %w", err)
        }
    }() 
	
    m.Run()
}

# Functions

NewContainer starts a new psql database in a docker container.
WithContainerName configures the PSQL Container Name, if empty, a random one will be picked.
WithDBPort sets database port running in the container, default 5432.
WithExpiration terminates the container after a period has passed.
WithImageTag configures the PSQL Container image tag, default: alpine.
WithPingRetryTimeout sets the timeout in seconds for the ping retry function.
WithPool sets the docker container getPool.
WithPoolEndpoint sets the docker container pool endpoint.
WithSQL specifies a sqls file, to initiate the db with.
WithTimescaleDB allows using the TimescaleDB repository and images.

# Variables

ErrWithPoolAndWithPoolEndpoint is returned when both WithPool and WithPoolEndpoint options are given to the NewContainer constructor.

# Structs

Container represents a Docker container running a PostgreSQL image.

# Interfaces

Option configures an BTC Node Docker.