{"id":19446093,"url":"https://github.com/stitchfix/go-postgres-testdb","last_synced_at":"2026-05-15T17:02:55.057Z","repository":{"id":143399462,"uuid":"106707909","full_name":"stitchfix/go-postgres-testdb","owner":"stitchfix","description":"Library for managing ephemeral test databases in Postgres","archived":false,"fork":false,"pushed_at":"2020-07-29T15:44:04.000Z","size":44,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-01-07T22:53:29.797Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stitchfix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-12T15:03:03.000Z","updated_at":"2021-01-18T12:04:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"47a66aed-e393-4607-921b-6dd210408c5d","html_url":"https://github.com/stitchfix/go-postgres-testdb","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fgo-postgres-testdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fgo-postgres-testdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fgo-postgres-testdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stitchfix%2Fgo-postgres-testdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stitchfix","download_url":"https://codeload.github.com/stitchfix/go-postgres-testdb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240630719,"owners_count":19832123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T16:12:54.752Z","updated_at":"2026-05-15T17:02:54.981Z","avatar_url":"https://github.com/stitchfix.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-postgres-test\n[![Circle CI](https://circleci.com/gh/stitchfix/go-postgres-testdb.svg?style=shield)](https://circleci.com/gh/stitchfix/go-postgres-testdb)\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/stitchfix/go-postgres-testdb)](https://goreportcard.com/report/github.com/stitchfix/go-postgres-testdb)\n\nA helper library for managing ephemeral test databases in Postgres.\n\nIt won't do anything if Postgres is not installed, but if the binaries are available, this library will allow you to create an ephemeral database, use it for the life of a test, and then clean up afterwards.\n\nIt was created cos there's currently no 'in memory' postgres clone that can be used for testing.\n\nIt's intended to be used on a laptop, or within an ephemeral container, not anyplace where a real production postgres instance is being used.\n\n\nIn a test, set up the following:\n\n        var tempDir string\n        var dbPid int\n        var dbDir string\n        var dbName string\n\n        func TestMain(m *testing.M) {\n            setUp()\n\n            code := m.Run()\n\n            tearDown()\n\n            os.Exit(code)\n        }\n\n        func setUp() {\n            dir, err := ioutil.TempDir(\"\", \"testdb\")\n            if err != nil {\n                fmt.Printf(\"Error creating temp dir %q: %s\", tempDir, err)\n                os.Exit(1)\n            }\n\n            tempDir = dir\n\n            dbName = \"fargle\"\n            dbDir = fmt.Sprintf(\"%s/%s\", tempDir, dbName)\n\n            dbPid, dbPort, err = StartTestDB(dbDir, dbName)\n            if err != nil {\n                fmt.Printf(\"Failed to start test db %q: %s\", dbName, err)\n            }\n            \n            running, err := PostgresRunning(dbPort)\n            if err != nil {\n                fmt.Printf(\"Error Checking to see if postgres is running: %s\", err)\n                os.Exit(1\n            }\n            if running {\n                fmt.Printf(\"Postgres is running with pid %d on port %d\", dbPid, dbPort)\n            }\n        }\n\n        func tearDown() {\n            err := StopPostgres(dbPid)\n            if err != nil {\n                fmt.Printf(\"Failed to stop postgres process %d\", dbPid)\n            } else {\n                fmt.Println(\"database shut down.\")\n            }\n\n            if _, err := os.Stat(tempDir); !os.IsNotExist(err) {\n                os.Remove(tempDir)\n            }\n\n        }\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstitchfix%2Fgo-postgres-testdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstitchfix%2Fgo-postgres-testdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstitchfix%2Fgo-postgres-testdb/lists"}