postgres.rst 3.8 KB

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