I wanted to incorporate Postgresql into my FastAPI API. After a research I decided to follow this tutorial - https://youtu.be/NH4VZaP3_9s And full code can be found here - https://github.com/veryacademy/YT_FastAPI_Beginner_Fast-Track
I checked several times and compared my code to tutorial one and I didn't find any differences.
But when I run command
docker-compose run my_api alembic revision --autogenerate -m "New Migration"
I got this output:
Creating my_api_my_api_run ... done
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'user'
INFO [alembic.autogenerate.compare] Detected added index 'ix_user_id' on '['id']'
INFO [alembic.autogenerate.compare] Detected added table 'captcha_solve_query'
INFO [alembic.autogenerate.compare] Detected added index 'ix_captcha_solve_query_id' on '['id']'
INFO [alembic.autogenerate.compare] Detected added table 'credits_transaction'
INFO [alembic.autogenerate.compare] Detected added index 'ix_credits_transaction_id' on '['id']'
Generating /my_api/alembic/versions/858d5482faf8_new_migration.py ... done
Yet my folder versions
is still empty. I tried to search my mac for this file 858d5482faf8_new_migration.py
but it didn't return anything.
Anyway, I tried going along and executed next:
docker-compose run my_api alembic upgrade head
And I received this output:
Creating my_api_my_api_run ... done
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
Which starts to be a little bit weird because in the tutorial the output has additional line similar to this:
Running upgrade -> 858d5482faf8 New, Migration
And I am missing this line, still no errors though.
After that when I am either checking the postgres manually or try to do request, I am seeing/getting errors of missing tables, like I wouldn't do any migrations.
I am not sure what's going on, since I followed tutorial and I am not seeing any errors. (I only get error about missing CSRF Token but I saw it as well in the tutorial so I guess it's not that?)
Could someone please point me in the right direction?
| | | | | EDIT | | | | | | | | | | EDIT | | | | | | | | | | EDIT | | | | |
I changed some things and kinda (?) managed to push it one step further (?). Commands from above still apply, but this time after I start docker I get this errors:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused Is the server running on host "postgresql_db" (192.168.80.2) and accepting TCP/IP connections on port 5432?
Here is my docker-compose.yml:
version: "3.8"
services:
database:
container_name: postgresql_db
image: postgres
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_EMAIL}
- PGADMIN_DEFAULT_PASSWORD=${PGADMIN_PASSWORD}
ports:
- "5050:80"
depends_on:
- database
my_api:
container_name: my_api
build: .
command: bash -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 8000 --reload"
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- database
restart: always
And here is my .env:
DATABASE_URL=postgresql+psycopg2://postgres:password@postgresql_db:5432/my_api_db
DB_USER=postgres
DB_PASSWORD=password
DB_NAME=my_api_db
PGADMIN_EMAIL=admin@admin.com
PGADMIN_PASSWORD=admin
I was googling about it, and common errors were using localhost instead of name of your container (that's not me I think), and specifying your postgresql address on home address (that's as well not me I think).