pgembed

package module
v0.0.0-...-0601f49 Latest Latest
Warning

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

Go to latest
Published: May 14, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

Go Wrapper For PostgreSQL Embedded

Install and run a PostgreSQL database locally on Linux or MacOS or Windows. PostgreSQL can be bundled with your application, or downloaded on demand.

See theseus-rs/postgresql-embedded for more information.

Supported Platforms
  • Linux x86_64
  • Darwin arm64

If you can get go generate to build the rust lib for other platforms, please send a PR.

Install
go get github.com/chirino/go-pgembed
Example
package main

import (
	"fmt"
	"log"

	"github.com/chirino/go-pgembed"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

func main() {
	pg, err := pgembed.New(pgembed.Config{
		Version:    "16.0.0",
		DataDir:    ".postgresql",
		RuntimeDir: ".postgresql",
	})
	if err != nil {
		log.Fatalf("failed to start embedded PostgreSQL: %v", err)
	}

	defer func() {
		if err := pg.Stop(); err != nil {
			log.Fatalf("failed to stop embedded PostgreSQL: %v", err)
		}
	}()

	err = pg.CreateDatabase("lanzadm", "lanzadm")
	if err != nil {
		log.Fatalf("failed to create db instance: %v", err)
	}
	dsn, err := pg.ConnectionString("lanzadm")

	db, err := sqlx.Connect("postgres", dsn)
	if err != nil {
		log.Fatalf("sqlx.Connect(%s) failed: %v", dsn, err)
	}
	defer db.Close()

	result := &struct{ Message string }{}
	if err := db.Get(result, `SELECT 'hello world' AS message`); err != nil {
		log.Fatalf("failed to query: %v", err)
	} else {
		fmt.Println("Message:", result.Message)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Version of PostgreSQL to use (e.g., "16.2.0", "15.6.0", "14.11.0", etc.). Mandatory.
	// Check postgresql-embedded crate for supported versions.
	Version string
	// DataDir is the path to the PostgreSQL data directory.
	// If empty, a temporary directory managed by the Rust library will be used.
	DataDir string
	// RuntimeDir is the path for runtime files (e.g., sockets).
	// If empty, a temporary directory managed by the Rust library will be used.
	RuntimeDir string
	// Port for PostgreSQL to listen on. If 0, a random available port will be chosen.
	Port uint16
	// Password for the default 'postgres' user. If empty, password may not be set or a default used.
	Password string
}

Config holds configuration for the embedded PostgreSQL.

type EmbeddedPostgres

type EmbeddedPostgres struct {
	// contains filtered or unexported fields
}

EmbeddedPostgres represents an embedded PostgreSQL instance.

func New

func New(config Config) (*EmbeddedPostgres, error)

New initializes, downloads (if necessary), and starts an embedded PostgreSQL instance. The first run for a specific PostgreSQL version might take time to download binaries. Binaries are cached by `postgresql-embedded` typically in `~/.embed-postgres/`.

func (*EmbeddedPostgres) ConnectionString

func (pg *EmbeddedPostgres) ConnectionString(dbName string) (string, error)

ConnectionString returns a libpq-compatible connection string for the given database name. If dbName is empty, "postgres" is typically used as the default database.

func (*EmbeddedPostgres) CreateDatabase

func (pg *EmbeddedPostgres) CreateDatabase(dbName string, owner string) error

CreateDatabase creates a new database in the embedded instance. The default owner is 'postgres' if owner string is empty.

func (*EmbeddedPostgres) DatabaseExists

func (pg *EmbeddedPostgres) DatabaseExists(dbName string) (bool, error)

DatabaseExists checks if a database with the given name exists.

func (*EmbeddedPostgres) DropDatabase

func (pg *EmbeddedPostgres) DropDatabase(dbName string) error

DropDatabase drops an existing database from the embedded instance.

func (*EmbeddedPostgres) Stop

func (pg *EmbeddedPostgres) Stop() error

Stop shuts down and cleans up the embedded PostgreSQL instance. It's safe to call Stop multiple times. This method is also registered as a finalizer for the EmbeddedPostgres struct.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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