-
Notifications
You must be signed in to change notification settings - Fork 873
draft: fix(docs): revise database procedures #4726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,28 +3,117 @@ title: Upgrading PostgreSQL to a new major version | |
| sidebar_label: Upgrading PostgreSQL | ||
| --- | ||
|
|
||
| 1. Create a [backup](backup_restore.md) | ||
| 2. Stop all TeslaMate containers | ||
| In these instructions it is assumed you are upgrading from postgresql 16 to postgresql 17. Please update | ||
| references to 16 to your start version and 17 to your desired version. | ||
|
|
||
| These instructions also assume you have the standard `docker-compose.yml` file. If this is not the case | ||
| you might need to make adjustments. For example, older installed might call the container "db" not "database". | ||
|
|
||
| :::note | ||
| If you are using `docker-compose`, you are using Docker Compose v1, which has been deprecated. Docker Compose commands refer to Docker Compose v2. Consider upgrading your docker setup, see [Migrate to Compose V2](https://docs.docker.com/compose/migrate/) | ||
| ::: | ||
|
|
||
| ## Upgrade | ||
|
|
||
| 1. Create a backup: | ||
|
|
||
| ```bash | ||
| docker compose exec -T database pg_dump -U teslamate teslamate > teslamate_database_$(date +%F_%H-%M-%S).sql | ||
| ``` | ||
|
|
||
| 2. _Check to make sure there are no obvious errors in this backup file._ There should not be any errors appearing | ||
| on screen. The file should be more than 0 bytes long. Do not proceed if there is any doubt. | ||
|
|
||
| 3. Stop all TeslaMate containers | ||
|
|
||
| ```bash | ||
| docker compose down | ||
| ``` | ||
|
|
||
| 3. Delete the database volume. **Be careful**, this will delete all your previously recorded data! Make sure that your backup can be restored before you start. | ||
| 4. Make a copy of your current `docker-compose.yml` | ||
|
|
||
| ```bash | ||
| docker volume rm "$(basename "$PWD")_teslamate-db" | ||
| cp docker-compose.yml docker-compose-16.yml | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any suggestions? Maybe just have text "Copy file docker-compose-16.yml over docker-compose.yml? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think cp should work on all OS we care about though, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should users backup the compose file. We could say - in case of error please revert your changes to the compose file to get back to a working version (that should be fine to ask for). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On one hand, I note that there is a comment that some environments may automatically delete any unexpected files, such as this backup. Oops. On the other hand, not really convinced our users are good at following written instructions... Think spelling it out might be required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those would also delete the postgres backup, right? as long as the data is still accessible everything is alright. if someone isn't able to follow a "revert changes made to docker-compose.yaml" file they still can open up a discussion / ask in forums for help. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These will delete the updated At least that is the theory. Possibly some platform might decide that since the data is no longer referenced it can be deleted. Hoping that doesn't actually happen... |
||
| ``` | ||
|
|
||
| 4. Change the postgres version in docker-compose.yml and start the container | ||
| 5. Update the source to point to a new location that doesn't exist already and update the postgres version. | ||
|
|
||
| ```yml {2} | ||
| database: | ||
| image: postgres:xx | ||
| ```yaml | ||
| services: | ||
| [...] | ||
| database: | ||
| image: postgres:17 | ||
| [...] | ||
| volumes: | ||
| - teslamate-db-17:/var/lib/postgresql/data | ||
brianmay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [...] | ||
| volumes: | ||
| teslamate-db-17: | ||
| ``` | ||
|
|
||
| Here we updated the image to `postgres:17` and the volume to `teslamate-db-17`. | ||
|
|
||
| :::note | ||
| There are THREE changes. All are very important. | ||
| ::: | ||
|
|
||
| :::note | ||
| Do not delete the old volume until you are sure that the database restore is | ||
| working correctly and has all your data. | ||
| ::: | ||
|
|
||
| 6. Start the container. | ||
|
|
||
| ```bash | ||
| docker compose up -d database | ||
| ``` | ||
|
|
||
| 5. [Restore](backup_restore.md) the backup | ||
| 7. Restore the backup. | ||
|
|
||
| :::note | ||
| database should be empty. If database contains data, then check you updated the volume correctly in step 5. | ||
| ::: | ||
|
|
||
| Replace `teslamate_database_2025-05-07_07-33-48.sql` with the actual filename generated during the backup step. | ||
|
|
||
| ```bash | ||
| docker compose exec -T database psql -U teslamate teslamate << . | ||
| CREATE SCHEMA public; | ||
| CREATE EXTENSION cube WITH SCHEMA public; | ||
| CREATE EXTENSION earthdistance WITH SCHEMA public; | ||
| . | ||
| DATABASE="teslamate_database_2025-05-07_07-33-48.sql" | ||
| docker compose exec -T database psql -U teslamate -d teslamate < "$DATABASE" | ||
| ``` | ||
|
|
||
| There should not be any errors. | ||
|
|
||
| 8. Start the container. | ||
|
|
||
| ```bash | ||
| docker compose up -d teslamate | ||
| ``` | ||
|
|
||
| 9. Make sure everything is working, and all the data is intact. | ||
|
|
||
| ## Roll back on error | ||
|
|
||
| If you cannot restore the backup for any reason, you might have to roll back to your previous version. | ||
|
|
||
| 1. Stop everything | ||
|
|
||
| ```bash | ||
| docker compose stop | ||
| ``` | ||
|
|
||
| 2. Restore the values in `docker-compose.yml` that you changed in step 5 and step 6. | ||
|
|
||
| ```bash | ||
| cp docker-compose-16.yml docker-compose.yml | ||
| ``` | ||
|
|
||
| 3. Start everything | ||
|
|
||
| ```bash | ||
| docker compose up -d | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using only a date (2025-05-16 here) - that way copy and paste of commands should still work (as users are most likely updating on the same day).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, not entirely sure what you are asking for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
going with
$(date +%Y-%m-%d)and not using %H-%M-%S will allow the restore command to be a copy / paste without users manually searching for the exact filename taken earlier (assuming that most upgrades happen at the same day).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question.
Was trying to avoid the situation here where you might have a good backup, but then you accidentally run the above command again after the database is shutdown/replaced and replace the good backup with a bad backup. And not notice the glaring error message saying it failed.
Yes, would like to think this wouldn't happen, but we did get a report from somebody who had a 0 byte backup file, and this is one way that could occur.
At least with the above, there is some increased chance you might still have the good backup file.