postgres.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. Using Postgres
  2. --------------
  3. Postgres version 9.5 or later is known to work.
  4. Install postgres client libraries
  5. =================================
  6. Synapse will require the python postgres client library in order to connect to
  7. a postgres database.
  8. * If you are using the `matrix.org debian/ubuntu
  9. packages <../INSTALL.md#matrixorg-packages>`_,
  10. the necessary python library will already be installed, but you will need to
  11. ensure the low-level postgres library is installed, which you can do with
  12. ``apt install libpq5``.
  13. * For other pre-built packages, please consult the documentation from the
  14. relevant package.
  15. * If you installed synapse `in a virtualenv
  16. <../INSTALL.md#installing-from-source>`_, you can install the library with::
  17. ~/synapse/env/bin/pip install matrix-synapse[postgres]
  18. (substituting the path to your virtualenv for ``~/synapse/env``, if you used a
  19. different path). You will require the postgres development files. These are in
  20. the ``libpq-dev`` package on Debian-derived distributions.
  21. Set up database
  22. ===============
  23. Assuming your PostgreSQL database user is called ``postgres``, create a user
  24. ``synapse_user`` with::
  25. su - postgres
  26. createuser --pwprompt synapse_user
  27. Before you can authenticate with the ``synapse_user``, you must create a
  28. database that it can access. To create a database, first connect to the database
  29. with your database user::
  30. su - postgres
  31. psql
  32. and then run::
  33. CREATE DATABASE synapse
  34. ENCODING 'UTF8'
  35. LC_COLLATE='C'
  36. LC_CTYPE='C'
  37. template=template0
  38. OWNER synapse_user;
  39. This would create an appropriate database named ``synapse`` owned by the
  40. ``synapse_user`` user (which must already have been created as above).
  41. Note that the PostgreSQL database *must* have the correct encoding set (as
  42. shown above), otherwise it will not be able to store UTF8 strings.
  43. You may need to enable password authentication so ``synapse_user`` can connect
  44. to the database. See https://www.postgresql.org/docs/11/auth-pg-hba-conf.html.
  45. Tuning Postgres
  46. ===============
  47. The default settings should be fine for most deployments. For larger scale
  48. deployments tuning some of the settings is recommended, details of which can be
  49. found at https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server.
  50. In particular, we've found tuning the following values helpful for performance:
  51. - ``shared_buffers``
  52. - ``effective_cache_size``
  53. - ``work_mem``
  54. - ``maintenance_work_mem``
  55. - ``autovacuum_work_mem``
  56. Note that the appropriate values for those fields depend on the amount of free
  57. memory the database host has available.
  58. Synapse config
  59. ==============
  60. When you are ready to start using PostgreSQL, edit the ``database`` section in
  61. your config file to match the following lines::
  62. database:
  63. name: psycopg2
  64. args:
  65. user: <user>
  66. password: <pass>
  67. database: <db>
  68. host: <host>
  69. cp_min: 5
  70. cp_max: 10
  71. All key, values in ``args`` are passed to the ``psycopg2.connect(..)``
  72. function, except keys beginning with ``cp_``, which are consumed by the twisted
  73. adbapi connection pool.
  74. Porting from SQLite
  75. ===================
  76. Overview
  77. ~~~~~~~~
  78. The script ``synapse_port_db`` allows porting an existing synapse server
  79. backed by SQLite to using PostgreSQL. This is done in as a two phase process:
  80. 1. Copy the existing SQLite database to a separate location (while the server
  81. is down) and running the port script against that offline database.
  82. 2. Shut down the server. Rerun the port script to port any data that has come
  83. in since taking the first snapshot. Restart server against the PostgreSQL
  84. database.
  85. The port script is designed to be run repeatedly against newer snapshots of the
  86. SQLite database file. This makes it safe to repeat step 1 if there was a delay
  87. between taking the previous snapshot and being ready to do step 2.
  88. It is safe to at any time kill the port script and restart it.
  89. Using the port script
  90. ~~~~~~~~~~~~~~~~~~~~~
  91. Firstly, shut down the currently running synapse server and copy its database
  92. file (typically ``homeserver.db``) to another location. Once the copy is
  93. complete, restart synapse. For instance::
  94. ./synctl stop
  95. cp homeserver.db homeserver.db.snapshot
  96. ./synctl start
  97. Copy the old config file into a new config file::
  98. cp homeserver.yaml homeserver-postgres.yaml
  99. Edit the database section as described in the section *Synapse config* above
  100. and with the SQLite snapshot located at ``homeserver.db.snapshot`` simply run::
  101. synapse_port_db --sqlite-database homeserver.db.snapshot \
  102. --postgres-config homeserver-postgres.yaml
  103. The flag ``--curses`` displays a coloured curses progress UI.
  104. If the script took a long time to complete, or time has otherwise passed since
  105. the original snapshot was taken, repeat the previous steps with a newer
  106. snapshot.
  107. To complete the conversion shut down the synapse server and run the port
  108. script one last time, e.g. if the SQLite database is at ``homeserver.db``
  109. run::
  110. synapse_port_db --sqlite-database homeserver.db \
  111. --postgres-config homeserver-postgres.yaml
  112. Once that has completed, change the synapse config to point at the PostgreSQL
  113. database configuration file ``homeserver-postgres.yaml``::
  114. ./synctl stop
  115. mv homeserver.yaml homeserver-old-sqlite.yaml
  116. mv homeserver-postgres.yaml homeserver.yaml
  117. ./synctl start
  118. Synapse should now be running against PostgreSQL.