Home Assistant with Postgres in docker
Last modified by Normann P. Nielsen on 2025/12/14 20:30
Install postgres via Docker (with data in /opt/pgdata)
mkdir /opt/pgdata
docker run -d \
--name postgres \
--restart always \
-p 5432:5432 \
-e POSTGRES_PASSWORD=****** \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v /opt/pgdata:/var/lib/postgresql/data \
postgres
docker run -d \
--name postgres \
--restart always \
-p 5432:5432 \
-e POSTGRES_PASSWORD=****** \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v /opt/pgdata:/var/lib/postgresql/data \
postgres
Install the Postgres client on Ubuntu and add the User and Database
sudo apt-get install postgresql-client
psql -U postgres -h 10.0.0.183
psql -U postgres -h 10.0.0.183
In the PSQL Console
CREATE ROLE hauser WITH LOGIN PASSWORD '******' VALID UNTIL 'infinity';
CREATE DATABASE homeassistant WITH ENCODING 'UTF-8' OWNER=hauser CONNECTION LIMIT=-1;
CREATE DATABASE homeassistant WITH ENCODING 'UTF-8' OWNER=hauser CONNECTION LIMIT=-1;
Add the configuration to the Home Assistant secrets file
hadbconfig: postgresql://hauser:*****@10.0.0.183/homeassistant
Add the config to configuration.yaml for "recorder"
recorder:
purge_keep_days: 30
db_url: !secret hadbconfig
purge_keep_days: 30
db_url: !secret hadbconfig
And restart Home Assistant