https://github.com/habx/pg-commands
Golang library: Postgres commands (pg_restore, pg_dumpg)
https://github.com/habx/pg-commands
golang postgresql
Last synced: 6 months ago
JSON representation
Golang library: Postgres commands (pg_restore, pg_dumpg)
- Host: GitHub
- URL: https://github.com/habx/pg-commands
- Owner: habx
- License: mit
- Created: 2020-08-27T12:16:41.000Z (almost 5 years ago)
- Default Branch: dev
- Last Pushed: 2023-05-02T13:35:05.000Z (about 2 years ago)
- Last Synced: 2024-06-18T17:07:41.253Z (about 1 year ago)
- Topics: golang, postgresql
- Language: Go
- Homepage:
- Size: 108 KB
- Stars: 48
- Watchers: 3
- Forks: 15
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pg-commands
[](https://codecov.io/gh/habx/pg-commands)
[](https://github.com/habx/pg-commands/releases/latest)
[](https://golang.org/doc/devel/release.html)
[](https://app.circleci.com/pipelines/github/habx/pg-commands)
[](/LICENSE)
[](https://pkg.go.dev/github.com/habx/pg-commands)## install
```bash
go get -t github.com/habx/pg-commands
```## Example
### Code
```go
dump, _ := pg.NewDump(&pg.Postgres{
Host: "localhost",
Port: 5432,
DB: "dev_example",
Username: "example",
Password: "example",
})
dumpExec := dump.Exec(pg.ExecOptions{StreamPrint: false})
if dumpExec.Error != nil {
fmt.Println(dumpExec.Error.Err)
fmt.Println(dumpExec.Output)} else {
fmt.Println("Dump success")
fmt.Println(dumpExec.Output)
}restore, _ := pg.NewRestore(&pg.Postgres{
Host: "localhost",
Port: 5432,
DB: "dev_example",
Username: "example",
Password: "example",
})
restoreExec := restore.Exec(dumpExec.File, pg.ExecOptions{StreamPrint: false})
if restoreExec.Error != nil {
fmt.Println(restoreExec.Error.Err)
fmt.Println(restoreExec.Output)} else {
fmt.Println("Restore success")
fmt.Println(restoreExec.Output)}
```### Lab
```
$ cd examples
$ docker-compose up -d
$ cd ..
$ POSTGRES_USER=example POSTGRES_PASSWORD=example POSTGRES_DB=postgres go run tests/fixtures/scripts/init-database/init-database.go$ go run main.go
Dump successRestore success
```