I have installed peewee and postgres. I am using FastAPI as the backend. When I run the request to create tables, it throws the shown error:
INFO: 127.0.0.1:33284 - "GET /setup_db HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
peewee.ImproperlyConfigured: Postgres driver not installed!
This is my main.py:
from fastapi import FastAPI
from models.schema import db, User, Plan
from datetime import datetime
from peewee import *
db = PostgresqlDatabase('database_name', host='localhost', port=5432, user='user', password='password')
app = FastAPI()
@app.get('/')
async def Home():
return "Welcome Home"
@app.get('/setup_db')
async def SetupDB():
db.connect()
db.create_tables([User],[Plan])
new_user = User.create(
username = 'user1',
email = 'user1@email.com',
hashed_password = 'pwd####',
create_on = datetime(2001, 3, 29, 5, 15, 30)
)
print(new_user)
return new_user
I tried importing the Models from different folders but it did not work. Please let me know what drivers/config do I need to add to make this work. Thanks & Cheers!
install postgresql driver:
pip install psycopg2
But, I suggest you to use more robust solution, like: sqlalchemy
, asyncpg
, alembic
:)