postgres.rst 4.9 KB

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