{"id":17867161,"url":"https://github.com/caarmen/fastapi-postgres-docker-example","last_synced_at":"2026-04-12T05:34:49.673Z","repository":{"id":158461317,"uuid":"633917296","full_name":"caarmen/fastapi-postgres-docker-example","owner":"caarmen","description":"Minimal example FastAPI app with Postgres, docker-compose","archived":false,"fork":false,"pushed_at":"2023-04-28T23:38:43.000Z","size":12,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T22:13:33.530Z","etag":null,"topics":["docker","docker-compose","fastapi","postgresql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/caarmen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-04-28T15:21:08.000Z","updated_at":"2024-05-05T17:28:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"61d6db93-4eea-40f6-bc86-2eb8e13d066f","html_url":"https://github.com/caarmen/fastapi-postgres-docker-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/caarmen/fastapi-postgres-docker-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caarmen%2Ffastapi-postgres-docker-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caarmen%2Ffastapi-postgres-docker-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caarmen%2Ffastapi-postgres-docker-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caarmen%2Ffastapi-postgres-docker-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/caarmen","download_url":"https://codeload.github.com/caarmen/fastapi-postgres-docker-example/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caarmen%2Ffastapi-postgres-docker-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31705574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-12T05:11:36.334Z","status":"ssl_error","status_checked_at":"2026-04-12T05:11:27.332Z","response_time":58,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["docker","docker-compose","fastapi","postgresql"],"created_at":"2024-10-28T09:44:39.483Z","updated_at":"2026-04-12T05:34:49.655Z","avatar_url":"https://github.com/caarmen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Example FastAPI app with Postgres, docker-compose\n\nMinimal example of a setup with docker-compose which\nstarts a postgres database and a FastAPI webapp.\n\nThe FastAPI webapp has one endpoint, which:\n* Inserts a row into a db table \"greetings\", which is\n  a string representation of the current timestamp.\n* Returns the list of all the rows of the table.\n\n\n☝️ The purpose of this repo is to note minimum wiring\nto have postgres and FastAPI configured in docker-compose.\nIt may not use best practices. (For example, the db password\nis committed to git in the .env file.)\n\n## Running\n\nBuild and run the both postgres and the FastAPI webapp:\n```\ndocker compose up --build\n```\n\nOpen the root endpoint at http://localhost:8000/\n\nOpen the api documentation at http://localhost:8000/redoc\n\n### Running only one component\nTo run only the database:\n```\ndocker compose up --build db\n```\n\nTo run only the FastAPI web app:\n```\ndocker compose up --build server\n```\n\n## Inspecting the database\n\nConnect to the database by opening an interactive session to\nthe docker container for the db:\n\n```\ndocker exec -it fastapi-postgres-docker-example-db-1 /usr/bin/psql --username=postgres --dbname=app\n```\n\nExample session to list all the greetings:\n```\n% docker exec -it fastapi-postgres-docker-example-db-1 /usr/bin/psql --username=postgres --dbname=app\n\npsql (15.2 (Debian 15.2-1.pgdg110+1))\nType \"help\" for help.\n\napp=# select * from greetings;\n id |            text            \n----+----------------------------\n  1 | 2023-04-28 15:18:02.150314\n  2 | 2023-04-28 15:31:12.426155\n(2 rows)\n```\n\n## Debugging the FastAPI app\n### Option 1 - Run the web app on the host, with the db in the docker container.\n\n* Install python on the host.\n* Create and activate a virtual environment with the tool of your choice (venv, pyenv, virtualenv...).\n* Install the dependencies: `pip install -r requirements.txt`\n* Set the following environment variables:\n  - `POSTGRES_SERVER=localhost`\n  - `POSTGRES_PORT=9432`\n* Run the webapp app in debug mode using your IDE/tool of choice, specifying `app/main.py` as the Python script to debug.\n* Open the root endpoint at http://localhost:8001/\n\nNote: the webapp run this way is run on port 8001, and can be run at the same time as the webapp\ninside the docker container, which runs on port 8000.\nHowever, if you don't want two webapps running, you can make sure that in\ndocker, only the db is started, with  `docker compose up --build db`.\n\n### Option 2 - Attach to the debugger inside the container, with vscode (with debugpy)\n* Rerun everything with `DEBUG` set to `debugpy` in the environment:\n    ```shell\n    env DEBUG=debugpy docker compose up --build\n    ```\n* Inside vscode, launch the `Python: Remote Attach` configuration.\n* Also, in vscode, you can use \"Remote explorer\" to browse the files inside the container.\n  That will open a new window. You can also launch the debugger from the new window.\n\n### Option 3 - Web-pdb\n* Add the following to the line of code you want to debug:\n    ```python\n    import web_pdb; web_pdb.set_trace()\n    ```\n* Rerun everything with `DEBUG` set to `pdb` in the environment:\n    ```shell\n    env DEBUG=pdb docker compose up --build\n    ```\n* Open http://localhost:5555/ to see the web-pdb interface.\n\n\n## References\n* [SQL (Relational) Databases in FastAPI](https://fastapi.tiangolo.com/tutorial/sql-databases/).\n* [FastAPI, Docker, and Postgres](https://medium.com/@krishnardt365/fastapi-docker-and-postgres-91943e71be92), by Krish Na.\n* [Docker compose healthcheck](https://github.com/peter-evans/docker-compose-healthcheck) to wait for postgres to be ready, before starting FastAPI.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaarmen%2Ffastapi-postgres-docker-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaarmen%2Ffastapi-postgres-docker-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaarmen%2Ffastapi-postgres-docker-example/lists"}