What Database Does Securo Use? PostgreSQL Explained with Code Examples
Securo uses PostgreSQL as its default production database, accessed asynchronously via the asyncpg driver through SQLAlchemy's async engine.
The open-source Securo finance platform is architected for high-performance async database operations. PostgreSQL serves as the primary data store, with SQLAlchemy 2.0's async ORM capabilities handling all database interactions. The codebase also supports SQLite for lightweight testing scenarios.
Securo Database Configuration
The database connection is centralized in the application's settings configuration. In backend/app/core/config.py, the Settings class defines the default database URL:
database_url: str = "postgresql+asyncpg://postgres:postgres@localhost:5432/securo"
This PostgreSQL + asyncpg combination enables fully asynchronous database operations without blocking the event loop.
How Securo Creates the Async Database Engine
The actual engine instantiation happens in backend/app/core/database.py. The code creates an AsyncEngine using SQLAlchemy's create_async_engine factory:
engine = create_async_engine(settings.database_url, echo=settings.debug)
Key parameters here:
settings.database_url— pulls from the configuration aboveecho=settings.debug— logs all SQL statements when debug mode is enabled
Database Session Management in Securo
Securo implements a dependency-injectable session pattern for FastAPI endpoints. The get_async_session generator yields managed AsyncSession instances:
# backend/app/core/database.py
from app.core.config import get_settings
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
settings = get_settings()
engine = create_async_engine(settings.database_url, echo=settings.debug)
async_session_maker = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async def get_async_session():
async with async_session_maker() as session:
yield session
The expire_on_commit=False setting optimizes performance by preventing automatic refresh after commits.
Using Securo's Database in FastAPI Endpoints
Endpoints receive sessions through FastAPI's dependency injection system. Here's the typical pattern found throughout the codebase:
from fastapi import APIRouter, Depends
from app.core.database import get_async_session
from sqlalchemy.ext.asyncio import AsyncSession
router = APIRouter()
@router.get("/accounts")
async def list_accounts(session: AsyncSession = Depends(get_async_session)):
result = await session.execute("SELECT * FROM account")
accounts = result.fetchall()
return accounts
This design ensures proper connection pooling and automatic session cleanup on request completion.
Database Testing: SQLite Alternative
While production runs on PostgreSQL, Securo's test suite uses an in-memory SQLite database for speed and isolation. This configuration lives in backend/tests/conftest.py, allowing unit tests to run without a live PostgreSQL instance.
Database Migrations with Alembic
Schema changes are managed through Alembic, configured in backend/alembic/env.py to use the same DATABASE_URL. Run migrations with:
alembic upgrade head
The migration scripts target the same PostgreSQL instance defined in the core configuration, ensuring consistency across environments.
Key Files Controlling Securo's Database
| Component | File Path | Purpose |
|---|---|---|
| Configuration | backend/app/core/config.py |
Defines Settings.database_url with PostgreSQL default |
| Engine Factory | backend/app/core/database.py |
Creates async SQLAlchemy engine and session maker |
| Test Database | backend/tests/conftest.py |
Configures SQLite for unit testing |
| Migrations | backend/alembic/env.py |
Alembic environment using production URL |
Summary
- Securo uses PostgreSQL as its production database with the
postgresql+asyncpgdriver - Async operations are handled through SQLAlchemy 2.0's
create_async_engineandAsyncSession - Configuration is centralized in
backend/app/core/config.pyvia thedatabase_urlsetting - Testing falls back to SQLite for faster test execution
- Migrations use Alembic pointing to the same PostgreSQL URL
Frequently Asked Questions
What database driver does Securo use for PostgreSQL?
Securo uses asyncpg as the underlying PostgreSQL driver. The connection string format postgresql+asyncpg:// tells SQLAlchemy to use this high-performance native async driver rather than psycopg2 or other alternatives.
Can Securo run with a different database?
Technically yes, but it requires code changes. The codebase is designed around PostgreSQL-specific features and the asyncpg driver. While SQLAlchemy abstracts some differences, switching to MySQL or another database would need verification of async driver compatibility and potential schema adjustments.
Why does Securo use async database operations?
The asyncpg + AsyncSession pattern prevents I/O blocking in FastAPI's async request handlers. This allows Securo to handle concurrent database operations efficiently without consuming threads per request, critical for financial applications requiring high throughput.
How do I change the Securo database connection?
Modify the DATABASE_URL environment variable or update the default in backend/app/core/config.py. The application reads this value at startup through Pydantic's settings management, with environment variables taking precedence over code defaults.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →