postgres

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Example

This example demonstrates starting a PostgreSQL container, creating a database, connecting via pgx, executing queries, and cleaning up.

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/jackc/pgx/v4"

	"github.com/teran/go-docker-testsuite/applications/postgres"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
	defer cancel()

	app, err := postgres.New(ctx)
	if err != nil {
		fmt.Printf("error: %v (is Docker running?)\n", err)
		return
	}
	defer func() { _ = app.Close(ctx) }()

	if err := app.CreateDB(ctx, "example_db"); err != nil {
		fmt.Printf("error creating database: %v\n", err)
		return
	}
	fmt.Println("database created")

	conn, err := pgx.Connect(ctx, app.MustDSN("example_db"))
	if err != nil {
		fmt.Printf("error connecting: %v\n", err)
		return
	}
	defer func() { _ = conn.Close(ctx) }()

	var result int
	if err := conn.QueryRow(ctx, "SELECT 42").Scan(&result); err != nil {
		fmt.Printf("error querying: %v\n", err)
		return
	}
	fmt.Printf("query returned %d\n", result)

	if err := app.DropDB(ctx, "example_db"); err != nil {
		fmt.Printf("error dropping database: %v\n", err)
		return
	}
	fmt.Println("database dropped")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PostgreSQL

type PostgreSQL interface {
	DSN(db string) (string, error)
	MustDSN(db string) string
	CreateDB(ctx context.Context, db string) error
	DropDB(ctx context.Context, db string) error
	Close(ctx context.Context) error
}

func New

func New(ctx context.Context) (PostgreSQL, error)

func NewWithImage

func NewWithImage(ctx context.Context, image string) (PostgreSQL, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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