You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # Using Postgres
  2. The minimum supported version of PostgreSQL is determined by the [Dependency
  3. Deprecation Policy](deprecation_policy.md).
  4. ## Install postgres client libraries
  5. Synapse will require the python postgres client library in order to
  6. connect to a postgres database.
  7. - If you are using the [matrix.org debian/ubuntu
  8. packages](setup/installation.md#matrixorg-packages), the necessary python
  9. library will already be installed, but you will need to ensure the
  10. low-level postgres library is installed, which you can do with
  11. `apt install libpq5`.
  12. - For other pre-built packages, please consult the documentation from
  13. the relevant package.
  14. - If you installed synapse [in a
  15. virtualenv](setup/installation.md#installing-as-a-python-module-from-pypi), you can install
  16. the library with:
  17. ~/synapse/env/bin/pip install "matrix-synapse[postgres]"
  18. (substituting the path to your virtualenv for `~/synapse/env`, if
  19. you used a different path). You will require the postgres
  20. development files. These are in the `libpq-dev` package on
  21. Debian-derived distributions.
  22. ## Set up database
  23. Assuming your PostgreSQL database user is called `postgres`, first authenticate as the database user with:
  24. ```sh
  25. su - postgres
  26. # Or, if your system uses sudo to get administrative rights
  27. sudo -u postgres bash
  28. ```
  29. Then, create a postgres user and a database with:
  30. ```sh
  31. # this will prompt for a password for the new user
  32. createuser --pwprompt synapse_user
  33. createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
  34. ```
  35. The above will create a user called `synapse_user`, and a database called
  36. `synapse`.
  37. Note that the PostgreSQL database *must* have the correct encoding set
  38. (as shown above), otherwise it will not be able to store UTF8 strings.
  39. You may need to enable password authentication so `synapse_user` can
  40. connect to the database. See
  41. <https://www.postgresql.org/docs/current/auth-pg-hba-conf.html>.
  42. ## Synapse config
  43. When you are ready to start using PostgreSQL, edit the `database`
  44. section in your config file to match the following lines:
  45. ```yaml
  46. database:
  47. name: psycopg2
  48. args:
  49. user: <user>
  50. password: <pass>
  51. dbname: <db>
  52. host: <host>
  53. cp_min: 5
  54. cp_max: 10
  55. ```
  56. All key, values in `args` are passed to the `psycopg2.connect(..)`
  57. function, except keys beginning with `cp_`, which are consumed by the
  58. twisted adbapi connection pool. See the [libpq
  59. documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS)
  60. for a list of options which can be passed.
  61. You should consider tuning the `args.keepalives_*` options if there is any danger of
  62. the connection between your homeserver and database dropping, otherwise Synapse
  63. may block for an extended period while it waits for a response from the
  64. database server. Example values might be:
  65. ```yaml
  66. database:
  67. args:
  68. # ... as above
  69. # seconds of inactivity after which TCP should send a keepalive message to the server
  70. keepalives_idle: 10
  71. # the number of seconds after which a TCP keepalive message that is not
  72. # acknowledged by the server should be retransmitted
  73. keepalives_interval: 10
  74. # the number of TCP keepalives that can be lost before the client's connection
  75. # to the server is considered dead
  76. keepalives_count: 3
  77. ```
  78. ## Tuning Postgres
  79. The default settings should be fine for most deployments. For larger
  80. scale deployments tuning some of the settings is recommended, details of
  81. which can be found at
  82. <https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server>.
  83. In particular, we've found tuning the following values helpful for
  84. performance:
  85. - `shared_buffers`
  86. - `effective_cache_size`
  87. - `work_mem`
  88. - `maintenance_work_mem`
  89. - `autovacuum_work_mem`
  90. Note that the appropriate values for those fields depend on the amount
  91. of free memory the database host has available.
  92. Additionally, admins of large deployments might want to consider using huge pages
  93. to help manage memory, especially when using large values of `shared_buffers`. You
  94. can read more about that [here](https://www.postgresql.org/docs/10/kernel-resources.html#LINUX-HUGE-PAGES).
  95. ## Porting from SQLite
  96. ### Overview
  97. The script `synapse_port_db` allows porting an existing synapse server
  98. backed by SQLite to using PostgreSQL. This is done in as a two phase
  99. process:
  100. 1. Copy the existing SQLite database to a separate location and run
  101. the port script against that offline database.
  102. 2. Shut down the server. Rerun the port script to port any data that
  103. has come in since taking the first snapshot. Restart server against
  104. the PostgreSQL database.
  105. The port script is designed to be run repeatedly against newer snapshots
  106. of the SQLite database file. This makes it safe to repeat step 1 if
  107. there was a delay between taking the previous snapshot and being ready
  108. to do step 2.
  109. It is safe to at any time kill the port script and restart it.
  110. However, under no circumstances should the SQLite database be `VACUUM`ed between
  111. multiple runs of the script. Doing so can lead to an inconsistent copy of your database
  112. into Postgres.
  113. To avoid accidental error, the script will check that SQLite's `auto_vacuum` mechanism
  114. is disabled, but the script is not able to protect against a manual `VACUUM` operation
  115. performed either by the administrator or by any automated task that the administrator
  116. may have configured.
  117. Note that the database may take up significantly more (25% - 100% more)
  118. space on disk after porting to Postgres.
  119. ### Using the port script
  120. Firstly, shut down the currently running synapse server and copy its
  121. database file (typically `homeserver.db`) to another location. Once the
  122. copy is complete, restart synapse. For instance:
  123. ```sh
  124. synctl stop
  125. cp homeserver.db homeserver.db.snapshot
  126. synctl start
  127. ```
  128. Copy the old config file into a new config file:
  129. ```sh
  130. cp homeserver.yaml homeserver-postgres.yaml
  131. ```
  132. Edit the database section as described in the section *Synapse config*
  133. above and with the SQLite snapshot located at `homeserver.db.snapshot`
  134. simply run:
  135. ```sh
  136. synapse_port_db --sqlite-database homeserver.db.snapshot \
  137. --postgres-config homeserver-postgres.yaml
  138. ```
  139. The flag `--curses` displays a coloured curses progress UI.
  140. If the script took a long time to complete, or time has otherwise passed
  141. since the original snapshot was taken, repeat the previous steps with a
  142. newer snapshot.
  143. To complete the conversion shut down the synapse server and run the port
  144. script one last time, e.g. if the SQLite database is at `homeserver.db`
  145. run:
  146. ```sh
  147. synapse_port_db --sqlite-database homeserver.db \
  148. --postgres-config homeserver-postgres.yaml
  149. ```
  150. Once that has completed, change the synapse config to point at the
  151. PostgreSQL database configuration file `homeserver-postgres.yaml`:
  152. ```sh
  153. synctl stop
  154. mv homeserver.yaml homeserver-old-sqlite.yaml
  155. mv homeserver-postgres.yaml homeserver.yaml
  156. synctl start
  157. ```
  158. Synapse should now be running against PostgreSQL.
  159. ## Troubleshooting
  160. ### Alternative auth methods
  161. If you get an error along the lines of `FATAL: Ident authentication failed for
  162. user "synapse_user"`, you may need to use an authentication method other than
  163. `ident`:
  164. * If the `synapse_user` user has a password, add the password to the `database:`
  165. section of `homeserver.yaml`. Then add the following to `pg_hba.conf`:
  166. ```
  167. host synapse synapse_user ::1/128 md5 # or `scram-sha-256` instead of `md5` if you use that
  168. ```
  169. * If the `synapse_user` user does not have a password, then a password doesn't
  170. have to be added to `homeserver.yaml`. But the following does need to be added
  171. to `pg_hba.conf`:
  172. ```
  173. host synapse synapse_user ::1/128 trust
  174. ```
  175. Note that line order matters in `pg_hba.conf`, so make sure that if you do add a
  176. new line, it is inserted before:
  177. ```
  178. host all all ::1/128 ident
  179. ```
  180. ### Fixing incorrect `COLLATE` or `CTYPE`
  181. Synapse will refuse to set up a new database if it has the wrong values of
  182. `COLLATE` and `CTYPE` set. Synapse will also refuse to start an existing database with incorrect values
  183. of `COLLATE` and `CTYPE` unless the config flag `allow_unsafe_locale`, found in the
  184. `database` section of the config, is set to true. Using different locales can cause issues if the locale library is updated from
  185. underneath the database, or if a different version of the locale is used on any
  186. replicas.
  187. If you have a database with an unsafe locale, the safest way to fix the issue is to dump the database and recreate it with
  188. the correct locale parameter (as shown above). It is also possible to change the
  189. parameters on a live database and run a `REINDEX` on the entire database,
  190. however extreme care must be taken to avoid database corruption.
  191. Note that the above may fail with an error about duplicate rows if corruption
  192. has already occurred, and such duplicate rows will need to be manually removed.
  193. ### Fixing inconsistent sequences error
  194. Synapse uses Postgres sequences to generate IDs for various tables. A sequence
  195. and associated table can get out of sync if, for example, Synapse has been
  196. downgraded and then upgraded again.
  197. To fix the issue shut down Synapse (including any and all workers) and run the
  198. SQL command included in the error message. Once done Synapse should start
  199. successfully.