postgres.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. Using Postgres
  2. --------------
  3. Postgres version 9.4 or later is known to work.
  4. Set up database
  5. ===============
  6. The PostgreSQL database used *must* have the correct encoding set, otherwise
  7. would not be able to store UTF8 strings. To create a database with the correct
  8. encoding use, e.g.::
  9. CREATE DATABASE synapse
  10. ENCODING 'UTF8'
  11. LC_COLLATE='C'
  12. LC_CTYPE='C'
  13. template=template0
  14. OWNER synapse_user;
  15. This would create an appropriate database named ``synapse`` owned by the
  16. ``synapse_user`` user (which must already exist).
  17. Set up client in Debian/Ubuntu
  18. ===========================
  19. Postgres support depends on the postgres python connector ``psycopg2``. In the
  20. virtual env::
  21. sudo apt-get install libpq-dev
  22. pip install psycopg2
  23. Set up client in RHEL/CentOs 7
  24. ==============================
  25. Make sure you have the appropriate version of postgres-devel installed. For a
  26. postgres 9.4, use the postgres 9.4 packages from
  27. [here](https://wiki.postgresql.org/wiki/YUM_Installation).
  28. As with Debian/Ubuntu, postgres support depends on the postgres python connector
  29. ``psycopg2``. In the virtual env::
  30. sudo yum install postgresql-devel libpqxx-devel.x86_64
  31. export PATH=/usr/pgsql-9.4/bin/:$PATH
  32. pip install psycopg2
  33. Synapse config
  34. ==============
  35. When you are ready to start using PostgreSQL, add the following line to your
  36. config file::
  37. database:
  38. name: psycopg2
  39. args:
  40. user: <user>
  41. password: <pass>
  42. database: <db>
  43. host: <host>
  44. cp_min: 5
  45. cp_max: 10
  46. All key, values in ``args`` are passed to the ``psycopg2.connect(..)``
  47. function, except keys beginning with ``cp_``, which are consumed by the twisted
  48. adbapi connection pool.
  49. Porting from SQLite
  50. ===================
  51. Overview
  52. ~~~~~~~~
  53. The script ``synapse_port_db`` allows porting an existing synapse server
  54. backed by SQLite to using PostgreSQL. This is done in as a two phase process:
  55. 1. Copy the existing SQLite database to a separate location (while the server
  56. is down) and running the port script against that offline database.
  57. 2. Shut down the server. Rerun the port script to port any data that has come
  58. in since taking the first snapshot. Restart server against the PostgreSQL
  59. database.
  60. The port script is designed to be run repeatedly against newer snapshots of the
  61. SQLite database file. This makes it safe to repeat step 1 if there was a delay
  62. between taking the previous snapshot and being ready to do step 2.
  63. It is safe to at any time kill the port script and restart it.
  64. Using the port script
  65. ~~~~~~~~~~~~~~~~~~~~~
  66. Firstly, shut down the currently running synapse server and copy its database
  67. file (typically ``homeserver.db``) to another location. Once the copy is
  68. complete, restart synapse. For instance::
  69. ./synctl stop
  70. cp homeserver.db homeserver.db.snapshot
  71. ./synctl start
  72. Assuming your new config file (as described in the section *Synapse config*)
  73. is named ``homeserver-postgres.yaml`` and the SQLite snapshot is at
  74. ``homeserver.db.snapshot`` then simply run::
  75. synapse_port_db --sqlite-database homeserver.db.snapshot \
  76. --postgres-config homeserver-postgres.yaml
  77. The flag ``--curses`` displays a coloured curses progress UI.
  78. If the script took a long time to complete, or time has otherwise passed since
  79. the original snapshot was taken, repeat the previous steps with a newer
  80. snapshot.
  81. To complete the conversion shut down the synapse server and run the port
  82. script one last time, e.g. if the SQLite database is at ``homeserver.db``
  83. run::
  84. synapse_port_db --sqlite-database homeserver.db \
  85. --postgres-config homeserver-postgres.yaml
  86. Once that has completed, change the synapse config to point at the PostgreSQL
  87. database configuration file ``homeserver-postgres.yaml`` (i.e. rename it to
  88. ``homeserver.yaml``) and restart synapse. Synapse should now be running against
  89. PostgreSQL.