1
0
mirror of https://github.com/spantaleev/matrix-docker-ansible-deploy.git synced 2025-06-12 22:17:28 +02:00
Files
.github
collections
docs
README.md
alternative-architectures.md
ansible.md
configuring-awx-system.md
configuring-captcha.md
configuring-dns.md
configuring-playbook-base-domain-serving.md
configuring-playbook-bot-go-neb.md
configuring-playbook-bot-matrix-reminder-bot.md
configuring-playbook-bot-mjolnir.md
configuring-playbook-bridge-appservice-discord.md
configuring-playbook-bridge-appservice-irc.md
configuring-playbook-bridge-appservice-slack.md
configuring-playbook-bridge-appservice-webhooks.md
configuring-playbook-bridge-beeper-linkedin.md
configuring-playbook-bridge-heisenbridge.md
configuring-playbook-bridge-matrix-bridge-sms.md
configuring-playbook-bridge-mautrix-facebook.md
configuring-playbook-bridge-mautrix-googlechat.md
configuring-playbook-bridge-mautrix-hangouts.md
configuring-playbook-bridge-mautrix-instagram.md
configuring-playbook-bridge-mautrix-signal.md
configuring-playbook-bridge-mautrix-telegram.md
configuring-playbook-bridge-mautrix-whatsapp.md
configuring-playbook-bridge-mx-puppet-discord.md
configuring-playbook-bridge-mx-puppet-groupme.md
configuring-playbook-bridge-mx-puppet-instagram.md
configuring-playbook-bridge-mx-puppet-skype.md
configuring-playbook-bridge-mx-puppet-slack.md
configuring-playbook-bridge-mx-puppet-steam.md
configuring-playbook-bridge-mx-puppet-twitter.md
configuring-playbook-client-element.md
configuring-playbook-client-hydrogen.md
configuring-playbook-dimension.md
configuring-playbook-dynamic-dns.md
configuring-playbook-email.md
configuring-playbook-email2matrix.md
configuring-playbook-etherpad.md
configuring-playbook-external-postgres.md
configuring-playbook-federation.md
configuring-playbook-jitsi.md
configuring-playbook-ldap-auth.md
configuring-playbook-ma1sd.md
configuring-playbook-matrix-corporal.md
configuring-playbook-matrix-registration.md
configuring-playbook-nginx.md
configuring-playbook-own-webserver.md
configuring-playbook-postgres-backup.md
configuring-playbook-prometheus-grafana.md
configuring-playbook-prometheus-postgres.md
configuring-playbook-rest-auth.md
configuring-playbook-riot-web.md
configuring-playbook-s3.md
configuring-playbook-shared-secret-auth.md
configuring-playbook-ssl-certificates.md
configuring-playbook-sygnal.md
configuring-playbook-synapse-admin.md
configuring-playbook-synapse-simple-antispam.md
configuring-playbook-synapse.md
configuring-playbook-telemetry.md
configuring-playbook-turn.md
configuring-playbook.md
configuring-well-known.md
container-images.md
faq.md
getting-the-playbook.md
howto-server-delegation.md
importing-postgres.md
importing-synapse-media-store.md
importing-synapse-sqlite.md
installing.md
maintenance-and-troubleshooting.md
maintenance-checking-services.md
maintenance-migrating.md
maintenance-postgres.md
maintenance-synapse.md
maintenance-upgrading-services.md
prerequisites.md
registering-users.md
self-building.md
uninstalling.md
updating-users-passwords.md
examples
group_vars
inventory
roles
.editorconfig
.gitignore
CHANGELOG.md
LICENSE
README.md
ansible.cfg
setup.yml
matrix-docker-ansible-deploy/docs/importing-postgres.md

103 lines
4.6 KiB
Markdown
Raw Normal View History

# Importing an existing Postgres database from another installation (optional)
Run this if you'd like to import your database from a previous installation.
(don't forget to import your Synapse `media_store` files as well - see [the importing-synape-media-store guide](importing-synapse-media-store.md)).
## Prerequisites
For this to work, **the database name in Postgres must match** what this playbook uses.
This playbook uses a Postgres database name of `synapse` by default (controlled by the `matrix_synapse_database_database` variable).
If your database name differs, be sure to change `matrix_synapse_database_database` to your desired name and to re-run the playbook before proceeding.
The playbook supports importing Postgres dump files in **text** (e.g. `pg_dump > dump.sql`) or **gzipped** formats (e.g. `pg_dump | gzip -c > dump.sql.gz`).
Importing multiple databases (as dumped by `pg_dumpall`) is also supported.
But the migration might be a good moment, to "reset" a not properly working bridge. Be aware, that it might affect all users (new link to bridge, new roomes, ...)
2019-01-01 15:35:33 +02:00
Before doing the actual import, **you need to upload your Postgres dump file to the server** (any path is okay).
## Importing
To import, run this command (make sure to replace `<server-path-to-postgres-dump.sql>` with a file path on your server):
```sh
ansible-playbook -i inventory/hosts setup.yml \
--extra-vars='server_path_postgres_dump=<server-path-to-postgres-dump.sql>' \
--tags=import-postgres
```
**Note**: `<server-path-to-postgres-dump.sql>` must be a file path to a Postgres dump file on the server (not on your local machine!).
## Troubleshooting
### Table Ownership
A table ownership issue can occur if you are importing from a Synapse installation which was both:
- migrated from SQLite to Postgres, and
- used a username other than 'synapse'
In this case you may run into the following error during the import task:
```
"ERROR: role \"synapse_user\" does not exist"
```
where `synapse_user` is the database username from the previous Synapse installation.
This can be verified by examining the dump for ALTER TABLE statements which set OWNER TO that username:
```Shell
$ grep "ALTER TABLE" homeserver.sql
ALTER TABLE public.access_tokens OWNER TO synapse_user;
ALTER TABLE public.account_data OWNER TO synapse_user;
ALTER TABLE public.account_data_max_stream_id OWNER TO synapse_user;
ALTER TABLE public.account_validity OWNER TO synapse_user;
ALTER TABLE public.application_services_state OWNER TO synapse_user;
...
```
It can be worked around by changing the username to `synapse`, for example by using `sed`:
2020-07-13 15:12:17 +01:00
```Shell
$ sed -i "s/OWNER TO synapse_user;/OWNER TO synapse;/g" homeserver.sql
```
This uses sed to perform an 'in-place' (`-i`) replacement globally (`/g`), searching for `synapse_user` and replacing with `synapse` (`s/synapse_user/synapse`). If your database username was different, change `synapse_user` to that username instead. Expand search/replace statement as shown in example above, in case of old user name like `matrix` - replacing `matrix` only would... well - you can imagine.
Note that if the previous import failed with an error it may have made changes which are incompatible with re-running the import task right away; if you do so it may fail with an error such as:
```
ERROR: relation \"access_tokens\" already exists
```
### Repeat import
In this case you can use the command suggested in the import task to clear the database before retrying the import:
```Shell
# systemctl stop matrix-postgres
# rm -rf /matrix/postgres/data/*
# systemctl start matrix-postgres
```
Now on your local machine run `ansible-playbook -i inventory/hosts setup.yml --tags=setup-postgres` to prepare the database roles etc.
If not, you probably get this error. `synapse` is the correct table owner, but the role is missing in database.
```
"ERROR: role synapse does not exist"
```
Once the database is clear and the ownership of the tables has been fixed in the SQL file, the import task should succeed.
Check, if `--dbname` is set to `synapse` (not `matrix`) and replace paths (or even better, copy this line from your terminal)
```
/usr/bin/env docker run --rm --name matrix-postgres-import --log-driver=none --user=998:1001 --cap-drop=ALL --network=matrix --env-file=/matrix/postgres/env-postgres-psql --mount type=bind,src=/migration/synapse_dump.sql,dst=/synapse_dump.sql,ro --entrypoint=/bin/sh docker.io/postgres:14.1-alpine -c "cat /synapse_dump.sql | grep -vE '^(CREATE|ALTER) ROLE (matrix)(;| WITH)' | grep -vE '^CREATE DATABASE (matrix)\s' | psql -v ON_ERROR_STOP=1 -h matrix-postgres --dbname=synapse"
```
### Hints
To open psql terminal run `/usr/local/bin/matrix-postgres-cli`