```sql CREATE DATABASE hass; CREATE USER hass WITH PASSWORD 'hass'; GRANT ALL PRIVILEGES ON DATABASE hass TO hass; ``` ``` recorder: db_url: postgresql://hass:hass@store.local/hass ``` Migrating your Home Assistant instance from SQLite to PostgreSQL involves a few steps. The process ensures all your historical state and event data from the existing SQLite database is preserved. --- ### **Step 1: Backup Your Current Home Assistant Instance** 1. **Stop Home Assistant**: ```bash sudo systemctl stop home-assistant ``` 2. **Create a Backup of Your SQLite Database**: - The database is typically located in the Home Assistant configuration directory (e.g., `/config/` or `/home/homeassistant/.homeassistant`). ```bash cp home-assistant_v2.db home-assistant_v2.db.backup ``` 3. **Backup Your Configuration Files**: ```bash tar -czvf home_assistant_config_backup.tar.gz /path/to/home-assistant/config ``` --- ### **Step 2: Install and Configure PostgreSQL** 1. **Install PostgreSQL**: ```bash sudo apt update sudo apt install postgresql ``` 2. **Create a Database for Home Assistant**: - Switch to the `postgres` user: ```bash sudo -i -u postgres ``` - Create the database and user: ```bash psql CREATE DATABASE hass; CREATE USER hass WITH PASSWORD 'hass'; GRANT ALL PRIVILEGES ON DATABASE hass TO hass; \q ``` - Exit the `postgres` user: ```bash exit ``` 3. **Test the Connection**: Use the `psql` client to connect: ```bash psql -h localhost -U hass -d hass ``` Enter the password you set earlier. If successful, you're ready to proceed. --- ### **Step 3: Install Required Tools** 1. **Install SQLite and PostgreSQL Clients**: ```bash sudo apt install sqlite3 postgresql-client ``` 2. **Install `pgloader`**: `pgloader` is a tool for migrating data between SQLite and PostgreSQL. ```bash sudo apt install pgloader ``` --- ### **Step 4: Migrate Data from SQLite to PostgreSQL** 1. **Prepare the `pgloader` Command**: Create a file called `migrate.load` with the following content: ```lisp LOAD DATABASE FROM sqlite://./home-assistant_v2.db.bak INTO postgresql://hass:hass@localhost/hass WITH data only, drop indexes, reset sequences, truncate; ALTER SCHEMA "main" RENAME TO "public"; ``` Replace `/path/to/home-assistant_v2.db` with the actual path to your SQLite database file. 2. **Run the Migration**: ```bash pgloader migrate.load ``` 3. **Verify the Data in PostgreSQL**: - Log in to PostgreSQL: ```bash psql -h localhost -U hass -d homeassistant ``` - Check the tables: ```sql \dt ``` --- ### **Step 5: Configure Home Assistant to Use PostgreSQL** 1. **Edit `configuration.yaml`**: Add the PostgreSQL database URL: ```yaml recorder: db_url: postgresql://hass:hass@192.168.55.53/hass ``` Replace `yourpassword` and `localhost` as needed. 2. **Restart Home Assistant**: ```bash sudo systemctl start home-assistant ``` 3. **Verify the Integration**: - Check the logs in Home Assistant for any database-related errors. - Confirm new data is being written to PostgreSQL by querying the `states` table: ```sql SELECT * FROM states ORDER BY last_updated DESC LIMIT 10; ``` --- ### **Step 6: Clean Up** 1. **Remove Old SQLite Database**: Once you confirm PostgreSQL is working, you can safely remove the SQLite database: ```bash rm home-assistant_v2.db ``` 2. **Optimize PostgreSQL**: - Configure PostgreSQL to improve performance: Edit `/etc/postgresql//main/postgresql.conf`: ```plaintext shared_buffers = 256MB work_mem = 16MB maintenance_work_mem = 64MB ``` - Restart PostgreSQL: ```bash sudo systemctl restart postgresql ``` --- ### **Final Notes** - Keep monitoring Home Assistant's logs during the first few days after migration to ensure the PostgreSQL setup is stable. - If needed, adjust the recorder settings in `configuration.yaml` to exclude entities or domains that generate excessive data: ```yaml recorder: include: domains: - sensor - switch exclude: entities: - sensor.unnecessary_metric ``` Let me know if you need assistance with any specific step! ``` ```sql LOAD DATABASE FROM mysql://root:数据库密码@localhost:3306/homeassistant INTO pgsql://homeassistant:数据库密码@localhost:5432/homeassistant WITH data only, workers = 8, concurrency = 1 CAST type datetime to timestamp drop default drop not null using zero-dates-to-null ; ``` ```bash sqlite3 home-assistant_v2.db.bak .dump > ha_dump.sql ``` ```bash sed -i 's/DATETIME/TIMESTAMP/g' ha_dump.sql ``` ```bash sed -i 's/BLOB/BYTEA/g' ha_dump.sql ``` ```bash psql -h localhost -U hass -d hass -f ha_dump.sql -W > load.log 2>&1 ``` ``` pgloader sqlite://./home-assistant_v2.db.bak postgresql://hass:hass@localhost/hass ``` ```sql CREATE SEQUENCE event_types_event_type_id_seq; CREATE SEQUENCE state_attributes_attributes_id_seq; CREATE SEQUENCE event_data_data_id_seq; CREATE SEQUENCE states_meta_metadata_id_seq; CREATE SEQUENCE statistics_meta_id_seq; CREATE SEQUENCE events_event_id_seq; CREATE SEQUENCE recorder_runs_run_id_seq; CREATE SEQUENCE schema_changes_change_id_seq; CREATE SEQUENCE statistics_runs_run_id_seq; CREATE SEQUENCE states_state_id_seq; CREATE SEQUENCE statistics_id_seq; CREATE SEQUENCE statistics_short_term_id_seq; SELECT setval('event_types_event_type_id_seq', MAX(event_type_id)) FROM event_types; SELECT setval('state_attributes_attributes_id_seq', MAX(attributes_id)) FROM state_attributes; SELECT setval('event_data_data_id_seq', MAX(data_id)) FROM event_data; SELECT setval('states_meta_metadata_id_seq', MAX(metadata_id)) FROM states_meta; SELECT setval('statistics_meta_id_seq', MAX(id)) FROM statistics_meta; SELECT setval('events_event_id_seq', MAX(event_id)) FROM events; SELECT setval('recorder_runs_run_id_seq', MAX(run_id)) FROM recorder_runs; SELECT setval('schema_changes_change_id_seq', MAX(change_id)) FROM schema_changes; SELECT setval('statistics_runs_run_id_seq', MAX(run_id)) FROM statistics_runs; SELECT setval('states_state_id_seq', MAX(state_id)) FROM states; SELECT setval('statistics_id_seq', MAX(id)) FROM statistics; SELECT setval('statistics_short_term_id_seq', MAX(id)) FROM statistics_short_term; ``` ``` recorder: db_url: postgresql://hass:hass@192.168.55.53/hass ``` ``` influxdb: host: 192.168.55.53 port: 8428 database: hass default_measurement: state ``` mysql: ```sql CREATE DATABASE hass; CREATE USER 'hass'@'%' IDENTIFIED BY 'hass'; GRANT ALL PRIVILEGES ON homeassistant.* TO 'hass'@'%'; FLUSH PRIVILEGES; ``` ``` sqlite3mysql --sqlite-file home-assistant_v2.db --mysql-user hass --mysql-password hass --mysql-database hass ``` ``` recorder: db_url: mysql://hass:hass@192.168.55.53/hass?charset=utf8mb4 ``` ``` influxdb: api_version: 1 host: 192.168.55.53 port: 8428 max_retries: 3 measurement_attr: entity_id tags_attributes: - friendly_name - unit_of_measurement ignore_attributes: - icon - source - options - editable - min - max - step - mode - marker_type - preset_modes - supported_features - supported_color_modes - effect_list - attribution - assumed_state - state_open - state_closed - writable - stateExtra - event - friendly_name - device_class - state_class - ip_address - device_file - unit_of_measurement - unitOfMeasure include: domains: - sensor - binary_sensor - light - switch - cover - climate - input_boolean - input_select - number - lock - weather exclude: entity_globs: - sensor.clock* - sensor.date* - sensor.glances* - sensor.time* - sensor.uptime* - sensor.dwd_weather_warnings_* - weather.weatherstation - binary_sensor.*_smartphone_* - sensor.*_smartphone_* - sensor.adguard_home_* - binary_sensor.*_internet_access ``` get sqlite schema ``` sqlite3 home-assistant_v2.db <