PGDump runs pg_dump against dbURL and returns the output. It is used by DumpOnFailure().
(dbURL string)
| 208 | // PGDump runs pg_dump against dbURL and returns the output. |
| 209 | // It is used by DumpOnFailure(). |
| 210 | func PGDump(dbURL string) ([]byte, error) { |
| 211 | if _, err := exec.LookPath("pg_dump"); err != nil { |
| 212 | return nil, xerrors.Errorf("could not find pg_dump in path: %w", err) |
| 213 | } |
| 214 | cmdArgs := []string{ |
| 215 | "pg_dump", |
| 216 | dbURL, |
| 217 | "--data-only", |
| 218 | "--column-inserts", |
| 219 | "--no-comments", |
| 220 | "--no-privileges", |
| 221 | "--no-publication", |
| 222 | "--no-security-labels", |
| 223 | "--no-subscriptions", |
| 224 | "--no-tablespaces", |
| 225 | // "--no-unlogged-table-data", // some tables are unlogged and may contain data of interest |
| 226 | "--no-owner", |
| 227 | "--exclude-table=schema_migrations", |
| 228 | } |
| 229 | cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) // nolint:gosec |
| 230 | cmd.Env = []string{ |
| 231 | // "PGTZ=UTC", // This is probably not going to be useful if tz has been changed. |
| 232 | "PGCLIENTENCODING=UTF8", |
| 233 | "PGDATABASE=", // we should always specify the database name in the connection string |
| 234 | } |
| 235 | var stdout, stderr bytes.Buffer |
| 236 | cmd.Stdout = &stdout |
| 237 | cmd.Stderr = &stderr |
| 238 | if err := cmd.Run(); err != nil { |
| 239 | return nil, xerrors.Errorf("exec pg_dump: %w\n%s", err, stderr.String()) |
| 240 | } |
| 241 | return stdout.Bytes(), nil |
| 242 | } |
| 243 | |
| 244 | const minimumPostgreSQLVersion = 13 |
| 245 |