pgdump

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 7 Imported by: 0

README

Go PostgreSQL Dump

Create PostgreSQL dumps in Go without the pg_dump CLI as a dependancy.

Inspired by go-mysqldump which does that but for MySQL/MariaDB.

Doesn't feature all of pg_dump features just yet (mainly around sequences) so it is still a work in progress.

Simple example using the library

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"time"

	"github.com/JCoupalK/go-pgdump"
)

func BackupPostgreSQL(username, password, hostname, dbname, outputDir string, port int) {
	// PostgreSQL connection string
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
		hostname, port, username, password, dbname)

	currentTime := time.Now()
	dumpFilename := filepath.Join(outputDir, fmt.Sprintf("%s-%s.sql", dbname, currentTime.Format("20060102T150405")))

	// Create a new dumper instance
	dumper := pgdump.NewDumper(psqlInfo)

	if err := dumper.DumpDatabase(dumpFilename); err != nil {
		fmt.Printf("Error dumping database: %v", err)
		os.Remove(dumpFilename) // Cleanup on failure
		return
	}

	fmt.Println("Backup successfully saved to", dumpFilename)
}

func main(){

	username := "user"
	password := "example"
	hostname := "examplehost"
	db := "dbname"
	outputDir := "path/to/example"
	port := 5432

	BackupPostgreSQL(username, password, hostname, db, outputDir, port)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DumpInfo

type DumpInfo struct {
	DumpVersion   string
	ServerVersion string
	CompleteTime  string
}

type Dumper

type Dumper struct {
	ConnectionString string
}

func NewDumper

func NewDumper(connectionString string) *Dumper

func (*Dumper) DumpDatabase

func (d *Dumper) DumpDatabase(outputFile string) error

Jump to

Keyboard shortcuts

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