Jan 8, 20232 min read
Create APIs with FastAPI and PostgreSQL
Python

To use FastAPI with PostgreSQL, you will need to install the PostgreSQL adapter for Python (psycopg2) and use it to connect to your PostgreSQL database from your FastAPI application.
Here is an example of how to do this:
import psycopg2
from fastapi import FastAPI
from pydantic import BaseModelapp = FastAPI()
DATABASE_URL = "postgresql://user:password@localhost:5432/database"
conn = psycopg2.connect(DATABASE_URL) cursor = conn.cursor()
class Item(BaseModel):
name: str
description: str
price: float
tax: float@app.post("/items/")
async def create_item(item: Item):
cursor.execute(
"INSERT INTO items (name, description, price, tax) VALUES (%s, %s, %s, %s) RETURNING id",
(item.name, item.description, item.price, item.tax)
)
item_id = cursor.fetchone()[0]
conn.commit()
return {"id": item_id}@app.get("/items/{item_id}")
async def read_item(item_id: int):
cursor.execute(
"SELECT name, description, price, tax FROM items WHERE id = %s",
(item_id,)
)
item = cursor.fetchone()
return {"id": item_id, "name": item[0], "description": item[1], "price": item[2], "tax": item[3]}@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
cursor.execute(
"UPDATE items SET name = %s, description = %s, price = %s, tax = %s WHERE id = %s",
(item.name, item.description, item.price, item.tax, item_id)
)
conn.commit()
return {"id": item_id, "name": item.name, "description": item.description, "price": item.price, "tax": item.tax}@app.delete("/items/{item_id}")
async def delete_item(item_id: int):
cursor.execute(
"DELETE FROM items WHERE id = %s",
(item_id,)
)
conn.commit()
return {"id": item_id}This is a basic example of how to use FastAPI with PostgreSQL to create a simple CRUD API. You can customize and extend this example as needed for your application.
You can find more information about using FastAPI with databases in the FastAPI documentation: https://fastapi.tiangolo.com/tutorial/sql-databases/
Thank You for reading!
You can Also Follow Me on My Social Media Platforms: