{"id":18438709,"url":"https://github.com/seapagan/fastapi_async_sqlalchemy2_example","last_synced_at":"2025-10-10T01:41:29.614Z","repository":{"id":219738245,"uuid":"657195848","full_name":"seapagan/fastapi_async_sqlalchemy2_example","owner":"seapagan","description":"A Simple example how to use FastAPI with Async SQLAlchemy 2.0","archived":false,"fork":false,"pushed_at":"2025-04-26T06:44:25.000Z","size":1429,"stargazers_count":15,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-26T07:25:57.688Z","etag":null,"topics":["async","example","fastapi","python","sqlalchemy","sqlalchemy-orm"],"latest_commit_sha":null,"homepage":"https://seapagan.github.io/fastapi_async_sqlalchemy2_example/","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/seapagan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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,"zenodo":null}},"created_at":"2023-06-22T14:20:56.000Z","updated_at":"2025-04-26T06:42:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"247dc904-8d9e-4ae4-b718-7862e007da82","html_url":"https://github.com/seapagan/fastapi_async_sqlalchemy2_example","commit_stats":null,"previous_names":["seapagan/fastapi_async_sqlalchemy2_example"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/seapagan/fastapi_async_sqlalchemy2_example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Ffastapi_async_sqlalchemy2_example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Ffastapi_async_sqlalchemy2_example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Ffastapi_async_sqlalchemy2_example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Ffastapi_async_sqlalchemy2_example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seapagan","download_url":"https://codeload.github.com/seapagan/fastapi_async_sqlalchemy2_example/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Ffastapi_async_sqlalchemy2_example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002399,"owners_count":26083374,"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","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["async","example","fastapi","python","sqlalchemy","sqlalchemy-orm"],"created_at":"2024-11-06T06:21:22.560Z","updated_at":"2025-10-10T01:41:29.580Z","avatar_url":"https://github.com/seapagan.png","language":"Python","readme":"# Using FastAPI with Async SQLAlchemy 2.0 \u003c!-- omit from toc --\u003e\n\n- [Introduction](#introduction)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Local Postgres server using Docker](#local-postgres-server-using-docker)\n  - [Use SQLite instead of PostgreSQL](#use-sqlite-instead-of-postgresql)\n- [License](#license)\n\n## Introduction\n\nI've been using [FastAPI](https://fastapi.tiangolo.com/) and\n[SQLAlchemy](https://www.sqlalchemy.org/) combined with\n[encode/databases](https://www.encode.io/databases/) for a while now.\n\nThe `databases` package is a great wrapper around `SQLAlchemy` that allows you\nto use async/await with SQLAlchemy.\n\nHowever, this does not seem be be actively maintained anymore. So I decided to\ngive the new [Async\nSQLAlchemy](https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html) a try\ninstead.\n\nThis repository contains a very simple example how to use FastAPI with Async\nSQLAlchemy 2.0.\n\n## Installation\n\nClone the repository and install the dependencies. This project uses\n[uv](https://docs.astral.sh/uv/) for dependency management which should be\ninstalled on your system first.\n\nInstall the project dependencies:\n\n```console\nuv sync\n```\n\nThen switch to the virtual environment:\n\n```console\nsource .venv/bin/activate\n```\n\n## Usage\n\nRun the server using `Uvicorn`:\n\n```console\nuvicorn main:app --reload\n```\n\n\u003e You can also run the server by just executing the `main.py` file:\n\u003e\n\u003e ```console\n\u003e python main.py\n\u003e ```\n\nThen open your browser at [http://localhost:8000](http://localhost:8000).\n\nThere is only one endpoint available: `/users`. It returns a list of all users\nfor a `GET` request and creates a new user for a `POST` request.\n\n### Local Postgres server using Docker\n\nThis example uses [PostgreSQL](https://www.postgresql.org/) as the database. If\nyou dont have a local PostgreSQL database running, you can start one with\n[Docker](https://www.docker.com) using the following command:\n\n```console\ndocker run \\\n  --rm   \\\n  --name  postgres \\\n  -p 5432:5432 \\\n  -e POSTGRES_USER=postgres \\\n  -e POSTGRES_PASSWORD=postgres \\\n  -e POSTGRES_DB=postgres \\\n  -d postgres\n```\n\nThis will run a PostgreSQL database in a Docker container in the background.\nWhen you are finished and want to stop the database, run:\n\n```console\ndocker stop postgres\n```\n\nIf needed, you can connect to the database managment by :\n\n```console\ndocker exec -it postgres psql -U postgres\n```\n\nThis will allow you to edit or delete the database or records.\n\n### Use SQLite instead of PostgreSQL\n\nFor testing purposes, you can also use SQLite instead of PostgreSQL. To do so,\nopen the [dp.py](db.py) file and comment out the PostgreSQL database in the\n`DATABASE_URL` environment variable and uncomment the SQLite database.\n\n```python\n# DATABASE_URL = \"postgresql+asyncpg://postgres:postgres@localhost/postgres\"\nDATABASE_URL = \"sqlite+aiosqlite:///./test.db\"\n```\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n\n```pre\nMIT License\n\nCopyright (c) 2023-2025 Grant Ramsay\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseapagan%2Ffastapi_async_sqlalchemy2_example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseapagan%2Ffastapi_async_sqlalchemy2_example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseapagan%2Ffastapi_async_sqlalchemy2_example/lists"}