why this code ? -------------------- - [https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/104](https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/104) - [https://github.com/tiangolo/fastapi/issues/551](https://github.com/tiangolo/fastapi/issues/551) P.S. How to quickly run postgres (using docker) -------------- This code was tested on Windows 11 WSL2 (Ubuntu VM) 1. `docker run -it --rm -p 5433:5432 --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -e PGDATA=/var/lib/postgresql/data -v /tmp/pgdata:/var/lib/postgresql/data -e POSTGRES_USER=test postgres` . This command will quickly start postgres on port 5433 and create a database *test* with user *test* and password *mysecretpassword*. The reason I like to use 5433 is because many times i have seen people having a normal, default installation on 5432 and it causes a lot of mistakes/confusion. 2. `docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -q)`. This will give you ip address of the postgres container. 3. `docker run --network=host -it --rm postgres psql postgresql://test:mysecretpassword@0.0.0.0:5433/test` . this will connect to your localhost on port 5433 cmdline -------------- `gunicorn sync_p:app -w 1 -k sync_p.RestartableUvicornWorker --logger-class sync_p.GunicornLogger` benchmark ---------------- ` docker run --network=host --rm skandyla/wrk -t12 -c400 -d30s http://localhost:8000/users/` **IMPORTANT** - do not forget the trailing slash (at the end of "users/". otherwise wrk will silently croak and u wont know. **IMPORTANT** - `expire_on_commit=False` is the important setting here. if u dont set it, the following error will happen ``` user = UserFactory() session.add(user) session.commit() # missing session.refresh(user) and causing the problem return user ``` created an object, added and ,committed it to the db and after that I tried to access on of the original object attributes without refreshing session session.refresh(object) **NOTE:** I have removed the async code from here. right now it is very unstable because of inherent issues in fastapi (like https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/290 and https://github.com/tiangolo/fastapi/issues/726#issuecomment-1025165337) . Using async in fastapi with sqlalchemy is absolutely not recommended right now.