postgres.rst 3.3 KB

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