Apache Airflow Database Connection Error Fix
In this tutorial, you'll learn about Apache Airflow Database Connection Error Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Airflow fails to start with SQLAlchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at localhost (127.0.0.1), port 5432 failed — the metadata database is unreachable or the connection string is wrong.
Step-by-Step Fix
1. Check the database connection URI
grep sql_alchemy_conn ~/airflow/airflow.cfg
Expected output:
sql_alchemy_conn = postgresql+psycopg2://airflow:password@localhost:5432/airflow
2. Test the database connection
psql -h localhost -p 5432 -U airflow -d airflow -c "SELECT 1"
Expected output:
?column?
----------
1
(1 row)
3. Fix common connection URI issues
# Wrong — missing database name
postgresql+psycopg2://airflow:password@localhost:5432/
# Wrong — wrong port
postgresql+psycopg2://airflow:password@localhost:3306/airflow
# Right — correct PostgreSQL URI
postgresql+psycopg2://airflow:password@localhost:5432/airflow
# Right — MySQL URI
mysql+mysqlconnector://airflow:password@localhost:3306/airflow
4. Initialize or upgrade the database
airflow db init
Expected output:
Initialization done.
Common Mistakes
| Mistake | Fix |
|---|---|
| Using SQLite for production | SQLite supports only single-threaded access. Use PostgreSQL for production. |
| Wrong PostgreSQL port | Default PostgreSQL port is 5432 (not 3306 which is MySQL). |
| Database does not exist | Create the database: CREATE DATABASE airflow; |
| Password contains special characters | URL-encode special characters: %40 for @, %25 for %. |
| SSL mode not configured | Append ?sslmode=require for SSL connections. |
Prevention
- Use PostgreSQL or MySQL as the metadata database backend.
- Configure connection pooling with
sql_alchemy_pool_size. - Set up database backup and restore procedures.
- Use environment variables for the connection URI:
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN.
DodaTech Tools
Doda Browser's developer console monitors database connection health for Airflow metadata stores. DodaZIP archives daily Airflow database backups with encryption. Durga Antivirus Pro scans connection strings and configuration files for exposed credentials.
Common Mistakes with connection error
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world AIRFLOW code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro