Difference between revisions of "Embedded postgresql using golang"
Jump to navigation
Jump to search
| Line 21: | Line 21: | ||
``` | ``` | ||
package main | package main | ||
| + | |||
/* | /* | ||
https://pkg.go.dev/github.com/fergusstrange/embedded-postgres#section-readme | https://pkg.go.dev/github.com/fergusstrange/embedded-postgres#section-readme | ||
| Line 29: | Line 30: | ||
import ( | import ( | ||
| − | + | "bytes" | |
| − | + | "fmt" | |
| − | + | "os" | |
| + | "strconv" | ||
| + | "time" | ||
| − | + | "github.com/drael/GOnetstat" | |
| + | "github.com/fergusstrange/embedded-postgres" | ||
| + | // "sync" | ||
) | ) | ||
| + | |||
| + | // var wg sync.WaitGroup | ||
func main() { | func main() { | ||
| − | + | port := 9999 | |
| + | // var port64 int64 | ||
| + | // port64 = int64(port) | ||
| + | // killPort(port64) | ||
| + | runseconds := 60 | ||
| + | var port32 uint32 | ||
| + | port32 = uint32(port) | ||
| + | // Version("14.2.0"). | ||
| + | // Version("13.6.0"). | ||
| + | // Version("12.10.0"). | ||
| + | logger := &bytes.Buffer{} | ||
| + | conf := embeddedpostgres.DefaultConfig(). | ||
| + | Username("demo"). | ||
| + | Password("demo"). | ||
| + | Database("demo"). | ||
// Version("14.2.0"). | // Version("14.2.0"). | ||
| − | + | Version("12.10.0"). | |
| − | + | RuntimePath("./runtime"). | |
| − | + | BinariesPath("./bin"). | |
| − | + | DataPath("./data"). | |
| − | + | BinaryRepositoryURL("https://repo1.maven.org/maven2"). | |
| − | + | Port(port32). | |
| − | + | StartTimeout(15 * time.Second). | |
| − | + | Logger(logger) | |
| − | + | ||
| − | + | postgres := embeddedpostgres.NewDatabase(conf) | |
| − | + | err := postgres.Start() | |
| − | + | if err != nil { | |
| − | + | fmt.Println(err) | |
| + | os.Exit(1) | ||
| + | } | ||
| + | |||
| + | fmt.Printf("Running postgres server for %d seconds.\n", runseconds) | ||
| + | time.Sleep(time.Duration(runseconds) * time.Second) | ||
| − | + | err = postgres.Stop() | |
| − | + | if err != nil { | |
| − | + | fmt.Println(err) | |
| − | + | } | |
| − | + | } | |
| − | |||
| − | |||
| − | err = | + | func killPort(port int64) { |
| − | if err != nil { | + | // defer wg.Done() |
| − | + | d := GOnetstat.Tcp() | |
| − | + | for _, p := range d { | |
| + | if p.State == "LISTEN" && p.Port == port { | ||
| + | fmt.Printf("Killing port, pid, name %v %v %v\n", port, p.Pid, p.Name) | ||
| + | pid, err := strconv.Atoi(p.Pid) | ||
| + | if err != nil { | ||
| + | panic(err.Error()) | ||
| + | } | ||
| + | process, err := os.FindProcess(pid) | ||
| + | if err != nil { | ||
| + | panic(err.Error()) | ||
| + | } | ||
| + | process.Signal(os.Interrupt) | ||
| + | fmt.Printf("Waiting 10 seconds for port to close.\n") | ||
| + | time.Sleep(time.Duration(10) * time.Second) | ||
| + | } | ||
| + | } | ||
} | } | ||
``` | ``` | ||
Latest revision as of 03:21, 12 March 2022
nano .env
export PGDATABASE=demo export PGHOST=127.0.0.1 export PGPASSWORD=demo export PGPORT=15432 export PGUSER=demo
Source
. .env
Get package
go get -u github.com/fergusstrange/embedded-postgres
nano runpg.go
package main
/*
https://pkg.go.dev/github.com/fergusstrange/embedded-postgres#section-readme
https://github.com/fergusstrange/embedded-postgres
https://github.com/zonkyio/embedded-postgres
https://mvnrepository.com/artifact/io.zonky.test.postgres/embedded-postgres-binaries-bom
*/
import (
"bytes"
"fmt"
"os"
"strconv"
"time"
"github.com/drael/GOnetstat"
"github.com/fergusstrange/embedded-postgres"
// "sync"
)
// var wg sync.WaitGroup
func main() {
port := 9999
// var port64 int64
// port64 = int64(port)
// killPort(port64)
runseconds := 60
var port32 uint32
port32 = uint32(port)
// Version("14.2.0").
// Version("13.6.0").
// Version("12.10.0").
logger := &bytes.Buffer{}
conf := embeddedpostgres.DefaultConfig().
Username("demo").
Password("demo").
Database("demo").
// Version("14.2.0").
Version("12.10.0").
RuntimePath("./runtime").
BinariesPath("./bin").
DataPath("./data").
BinaryRepositoryURL("https://repo1.maven.org/maven2").
Port(port32).
StartTimeout(15 * time.Second).
Logger(logger)
postgres := embeddedpostgres.NewDatabase(conf)
err := postgres.Start()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Running postgres server for %d seconds.\n", runseconds)
time.Sleep(time.Duration(runseconds) * time.Second)
err = postgres.Stop()
if err != nil {
fmt.Println(err)
}
}
func killPort(port int64) {
// defer wg.Done()
d := GOnetstat.Tcp()
for _, p := range d {
if p.State == "LISTEN" && p.Port == port {
fmt.Printf("Killing port, pid, name %v %v %v\n", port, p.Pid, p.Name)
pid, err := strconv.Atoi(p.Pid)
if err != nil {
panic(err.Error())
}
process, err := os.FindProcess(pid)
if err != nil {
panic(err.Error())
}
process.Signal(os.Interrupt)
fmt.Printf("Waiting 10 seconds for port to close.\n")
time.Sleep(time.Duration(10) * time.Second)
}
}
}
Build
go build runpg.go
Run
./runpg