Home Assistant logo

How to move the Home Assistant SQLite database in Docker on Windows?

This post was most recently updated on May 10th, 2022.

3 min read.

This article explains how you can move your Home Assistant’s SQLite database files to another location by mapping them to another volume in Docker. Using Docker on Windows. Which might not be a smart move, but it’s one I went with anyway.

The article will explain how to do this with docker-compose, but the steps pretty much apply to vanilla docker as well.

With that said – let’s move on!

Background

Like so many times before, I was quite annoyed by the relatively small number of useful results when looking for solutions whilst running Home Assistant in Docker, especially on Windows. I guess that the people running it in Docker is a pretty small group, and those stupid enough to try and make it happen on Windows are few and far between?

Anyway – through trial and error, I figured it out, and as usual, would love to share the solution with you!

But why would you want to do this?

Reason

The database might grow somewhat quickly, and it (and the accompanying temporary files) are quite volatile – they get written and read all the time. In case the rest of your Home Assistant is installed on a slow SD card or traditional hard drive, or even a USB stick, you might want to stash your database files on something with a better performance.

Or maybe you’ll just want to version control most of your files – just not the database. Backing that up should be a separate process.

In any case, moving the database files to another volume solves the issue!

Solution

So here are the steps on how to move your Home Assistant’s SQLite database to another location from the rest of the files. The steps are written for Docker on Windows when using docker-compose, but with some adjustment should probably work for other configurations :)

Time needed: 15 minutes

How to move your Home Assistant SQLite database when using Docker on Windows?

  1. Stop your Home Assistant container

    Stopping the container shouldn’t take us too long – you can do this from Docker Desktop or by running:

    docker-compose stop

    In the directory where your docker-compose.yml file for Docker is, or obviously just:

    docker stop [containername]

  2. Verify all of the temporary files have disappeared

    Let’s make sure no temporary files are left over. Navigate to your database folder, and make sure the .db-shm and .db-wal files disappear!

    If not, I’d perhaps restart the container once, make sure it does whatever it needs to do for cleanup and then shut it down again. If files are still leftover, they’re probably safe to remove.

  3. Map a new volume for your Home Assistant container

    Mapping a volume to a container is one of the things you need to do constantly with pretty much any container orchestration method. I’m using Docker Compose, and this is how you can do it:

    version: '3'
    services:
    homeassistant:
    container_name: home-assistant
    image: homeassistant/home-assistant:2021.9.0
    privileged: yes
    volumes:
    - C:/[yourpath]/config/HA/HomeAssistant:/config # Shares the configuration directory
    - C:/HA_db:/db # Shares the new database folder
    environment:
    - TZ=Europe/Helsinki
    restart: always
    ports:
    - 8123:8123

  4. Move your database file to the new volume

    Now we move our database file.

    I guess this step is pretty straightforward, as you should know which path you mapped in the previous step.

  5. Modify your configuration.yaml

    Now we’ll modify the the configuration for Home Assistant!

    Add recorder configuration like this:

    # Change database location:
    # https://www.home-assistant.io/integrations/recorder/
    recorder:
    db_url: sqlite:////db/home-assistant_v2.db


    The ridiculous-looking path has first the protocol – sqlite:// – and then (I guess) the escaped volume path. Took me a while to figure the right number of forward slashes out :)

  6. Start your Home Assistant container

    Let’s get the container back up and running!

    Basically:
    docker-compose start

    Or:
    docker start [containername]

And you should be good! Verify that all is well by browsing to History – view and make sure, that your entities have a history longer than just a few minutes. And if they don’t – check what’s in the log.

mm
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments