Boost Home Assistant Performance: Migrating the Recorder to PostgreSQL

NGC 224
DIY Smart Home Creator
Home Assistant, the open-source smart home platform, relies on its recorder
integration to store historical data from entities. This data powers features like the history graphs, logbook, and certain automations. By default, Home Assistant uses a SQLite database file (home-assistant_v2.db
) stored directly within its configuration directory.
Why SQLite Can Become a Bottleneck
For small or new installations, SQLite performs perfectly well. It's lightweight, easy to set up (it requires no external server), and works out-of-the-box. However, as your Home Assistant instance grows – you add more devices, more entities, and keep history for longer periods – the SQLite database file can become very large. SQLite is designed for simpler, single-user access. When multiple processes (like the Home Assistant frontend, backend, and integrations) try to read from or write to a large SQLite database concurrently, performance can degrade significantly. This can manifest as slow loading history pages, delays in the UI, and warnings in the Home Assistant logs about database operations taking too long.
The Advantage of an External Database: PostgreSQL
External relational databases like PostgreSQL or MariaDB/MySQL are built for concurrent access, large datasets, and high performance. They use sophisticated indexing, caching, and query optimization techniques that SQLite doesn't. Migrating the Home Assistant recorder
to an external database offers several key benefits:
- Improved Performance: Faster loading of history, logbook, and quicker database operations overall.
- Enhanced Reliability: External databases are generally more resilient to corruption compared to a large SQLite file, especially under heavy load or unexpected shutdowns.
- Scalability: Better suited to handle the increasing data volume as your smart home grows.
- Separation of Concerns: Moving the database off the main Home Assistant process can free up resources.
- Centralized Management: If you already run a database server, you can manage your Home Assistant data alongside other applications.
While MariaDB/MySQL are also excellent choices, this guide will focus on PostgreSQL, another popular and powerful open-source database.
Prerequisites: Setting Up PostgreSQL
Before you can configure Home Assistant, you need a running PostgreSQL server and a dedicated database and user for Home Assistant. You can set this up in various ways:
- On a separate server or a dedicated machine (e.g., a Raspberry Pi).
- In a Docker container on the same machine running Home Assistant (if using Docker Compose or similar).
- Using a managed database service (though less common for home users).
General Steps to Create Database and User (using psql command-line tool):
# Connect as a superuser (e.g., postgres user)
psql -U postgres
# Create a new database for Home Assistant
CREATE DATABASE homeassistant;
# Create a new user for Home Assistant
CREATE USER ha_user WITH PASSWORD 'your_secure_password';
# Grant privileges on the database to the user
GRANT ALL PRIVILEGES ON DATABASE homeassistant TO ha_user;
# Optional: Connect to the new database and grant privileges on future tables
\c homeassistant
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ha_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ha_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ha_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO ha_user;
# Exit psql
\q
Important: Replace 'your_secure_password'
with a strong, unique password. Note the database name (homeassistant
), username (ha_user
), and password. You'll also need the hostname or IP address of the PostgreSQL server and the port (default is 5432).
Configuring Home Assistant
Now that your PostgreSQL database is ready, you need to tell Home Assistant to use it.
-
Stop Home Assistant: It's crucial to stop Home Assistant gracefully before modifying configuration files that affect core components like the recorder.
-
Edit your
configuration.yaml
file: Add or modify therecorder:
section.You need to add a
db_url
parameter with the connection string for your PostgreSQL database. The format is:recorder: db_url: postgresql://USERNAME:PASSWORD@HOST:PORT/DATABASE
Replace the placeholders with your database details:
USERNAME
: The PostgreSQL username (e.g.,ha_user
)PASSWORD
: The user's passwordHOST
: The hostname or IP address of the PostgreSQL serverPORT
: The PostgreSQL port (default is5432
)DATABASE
: The database name (e.g.,homeassistant
)
Example:
recorder: db_url: postgresql://ha_user:[email protected]:5432/homeassistant # Optional: Exclude noisy entities to keep the database size manageable exclude: entities: - sensor.last_boot - sensor.date domains: - automation # Often useful to exclude automation trigger/action data if not needed in history
Security Note: Putting credentials directly in
configuration.yaml
is not ideal for security. A better practice is to use Home Assistant Secrets. Create asecrets.yaml
file (if you don't have one) and add an entry like:recorder_db_url: postgresql://ha_user:[email protected]:5432/homeassistant
Then, reference the secret in your
configuration.yaml
:recorder: db_url: !secret recorder_db_url # ... other recorder options
-
(Optional) Configure Purging: Ensure you have
purge_keep_days
set appropriately in your recorder configuration to automatically remove old data. This is crucial for keeping the database size under control, regardless of whether you use SQLite or an external database.recorder: db_url: !secret recorder_db_url purge_keep_days: 7 # Keep only 7 days of history # ... other recorder options
-
Start Home Assistant: Start Home Assistant again. It should now connect to the PostgreSQL database and begin writing data there.
Verification
How do you know it's working?
- Check the Home Assistant logs for any errors related to the
recorder
integration or database connections. - Use PostgreSQL tools (like
psql
, pgAdmin, or similar) to connect to the database and verify that new tables (prefixed withstates
andevents
) are being created and populated as Home Assistant runs. - Check that the old
home-assistant_v2.db
file in your configuration directory is no longer being updated after you stop and start HA. You can eventually delete it once you are confident the new database is working (consider backing it up first). - Verify that your History and Logbook views in the Home Assistant frontend are populating correctly.
Maintaining a Reliable Database
Migrating to PostgreSQL improves reliability, but it also shifts responsibility for database management to you. Consider these best practices:
- Regular Backups: Implement a strategy to regularly back up your PostgreSQL database. This is critical data! Tools like
pg_dump
can be automated via cron jobs or systemd timers. - Database Size Management: Regularly review your
recorder
configuration. Exclude entities or domains that generate excessive, unneeded data (e.g., sensors that update constantly with minor changes). Adjustpurge_keep_days
based on how much history you truly need. - Monitor Database Health: Keep an eye on disk space usage on your database server and basic performance metrics.
- Database Server Reliability: Ensure the server hosting PostgreSQL is stable, has sufficient resources (CPU, RAM, storage), and is on a reliable network connection to your Home Assistant instance.
Troubleshooting
- Connection Errors: Double-check the
db_url
format, hostname/IP, port, username, and password. Ensure the PostgreSQL server is running and accessible from the machine running Home Assistant (check firewalls!). - Permission Issues: Ensure the database user has the necessary permissions to create tables and write data in the specified database.
- Home Assistant Won't Start: If a database configuration error prevents HA from starting, you may need to revert
configuration.yaml
via command line or SSH access.
Conclusion
Migrating the Home Assistant recorder
database from SQLite to PostgreSQL (or MariaDB/MySQL) is a significant step towards a more robust and performant smart home system, especially as your installation grows. While it requires setting up and managing an external database server, the benefits in terms of speed and reliability are well worth the effort for many users. By following the setup steps and best practices outlined, you can ensure your historical data is stored efficiently and reliably, leading to a smoother Home Assistant experience.

NGC 224
Author bio: DIY Smart Home Creator