Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 6660912

Browse files
authored
Update postgres docs (#9989)
1 parent 6482075 commit 6660912

File tree

3 files changed

+98
-103
lines changed

3 files changed

+98
-103
lines changed

changelog.d/9988.doc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fix outdated minimum PostgreSQL version in postgres.md.
1+
Updates to the PostgreSQL documentation (`postgres.md`).

changelog.d/9989.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updates to the PostgreSQL documentation (`postgres.md`).

docs/postgres.md

Lines changed: 96 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,15 @@ Assuming your PostgreSQL database user is called `postgres`, first authenticate
3333
# Or, if your system uses sudo to get administrative rights
3434
sudo -u postgres bash
3535

36-
Then, create a user ``synapse_user`` with:
36+
Then, create a postgres user and a database with:
3737

38+
# this will prompt for a password for the new user
3839
createuser --pwprompt synapse_user
3940

40-
Before you can authenticate with the `synapse_user`, you must create a
41-
database that it can access. To create a database, first connect to the
42-
database with your database user:
41+
createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
4342

44-
su - postgres # Or: sudo -u postgres bash
45-
psql
46-
47-
and then run:
48-
49-
CREATE DATABASE synapse
50-
ENCODING 'UTF8'
51-
LC_COLLATE='C'
52-
LC_CTYPE='C'
53-
template=template0
54-
OWNER synapse_user;
55-
56-
This would create an appropriate database named `synapse` owned by the
57-
`synapse_user` user (which must already have been created as above).
43+
The above will create a user called `synapse_user`, and a database called
44+
`synapse`.
5845

5946
Note that the PostgreSQL database *must* have the correct encoding set
6047
(as shown above), otherwise it will not be able to store UTF8 strings.
@@ -63,79 +50,6 @@ You may need to enable password authentication so `synapse_user` can
6350
connect to the database. See
6451
<https://www.postgresql.org/docs/current/auth-pg-hba-conf.html>.
6552

66-
If you get an error along the lines of `FATAL: Ident authentication failed for
67-
user "synapse_user"`, you may need to use an authentication method other than
68-
`ident`:
69-
70-
* If the `synapse_user` user has a password, add the password to the `database:`
71-
section of `homeserver.yaml`. Then add the following to `pg_hba.conf`:
72-
73-
```
74-
host synapse synapse_user ::1/128 md5 # or `scram-sha-256` instead of `md5` if you use that
75-
```
76-
77-
* If the `synapse_user` user does not have a password, then a password doesn't
78-
have to be added to `homeserver.yaml`. But the following does need to be added
79-
to `pg_hba.conf`:
80-
81-
```
82-
host synapse synapse_user ::1/128 trust
83-
```
84-
85-
Note that line order matters in `pg_hba.conf`, so make sure that if you do add a
86-
new line, it is inserted before:
87-
88-
```
89-
host all all ::1/128 ident
90-
```
91-
92-
### Fixing incorrect `COLLATE` or `CTYPE`
93-
94-
Synapse will refuse to set up a new database if it has the wrong values of
95-
`COLLATE` and `CTYPE` set, and will log warnings on existing databases. Using
96-
different locales can cause issues if the locale library is updated from
97-
underneath the database, or if a different version of the locale is used on any
98-
replicas.
99-
100-
The safest way to fix the issue is to take a dump and recreate the database with
101-
the correct `COLLATE` and `CTYPE` parameters (as shown above). It is also possible to change the
102-
parameters on a live database and run a `REINDEX` on the entire database,
103-
however extreme care must be taken to avoid database corruption.
104-
105-
Note that the above may fail with an error about duplicate rows if corruption
106-
has already occurred, and such duplicate rows will need to be manually removed.
107-
108-
109-
## Fixing inconsistent sequences error
110-
111-
Synapse uses Postgres sequences to generate IDs for various tables. A sequence
112-
and associated table can get out of sync if, for example, Synapse has been
113-
downgraded and then upgraded again.
114-
115-
To fix the issue shut down Synapse (including any and all workers) and run the
116-
SQL command included in the error message. Once done Synapse should start
117-
successfully.
118-
119-
120-
## Tuning Postgres
121-
122-
The default settings should be fine for most deployments. For larger
123-
scale deployments tuning some of the settings is recommended, details of
124-
which can be found at
125-
<https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server>.
126-
127-
In particular, we've found tuning the following values helpful for
128-
performance:
129-
130-
- `shared_buffers`
131-
- `effective_cache_size`
132-
- `work_mem`
133-
- `maintenance_work_mem`
134-
- `autovacuum_work_mem`
135-
136-
Note that the appropriate values for those fields depend on the amount
137-
of free memory the database host has available.
138-
13953
## Synapse config
14054

14155
When you are ready to start using PostgreSQL, edit the `database`
@@ -165,18 +79,42 @@ may block for an extended period while it waits for a response from the
16579
database server. Example values might be:
16680

16781
```yaml
168-
# seconds of inactivity after which TCP should send a keepalive message to the server
169-
keepalives_idle: 10
82+
database:
83+
args:
84+
# ... as above
85+
86+
# seconds of inactivity after which TCP should send a keepalive message to the server
87+
keepalives_idle: 10
17088
171-
# the number of seconds after which a TCP keepalive message that is not
172-
# acknowledged by the server should be retransmitted
173-
keepalives_interval: 10
89+
# the number of seconds after which a TCP keepalive message that is not
90+
# acknowledged by the server should be retransmitted
91+
keepalives_interval: 10
17492
175-
# the number of TCP keepalives that can be lost before the client's connection
176-
# to the server is considered dead
177-
keepalives_count: 3
93+
# the number of TCP keepalives that can be lost before the client's connection
94+
# to the server is considered dead
95+
keepalives_count: 3
17896
```
17997

98+
## Tuning Postgres
99+
100+
The default settings should be fine for most deployments. For larger
101+
scale deployments tuning some of the settings is recommended, details of
102+
which can be found at
103+
<https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server>.
104+
105+
In particular, we've found tuning the following values helpful for
106+
performance:
107+
108+
- `shared_buffers`
109+
- `effective_cache_size`
110+
- `work_mem`
111+
- `maintenance_work_mem`
112+
- `autovacuum_work_mem`
113+
114+
Note that the appropriate values for those fields depend on the amount
115+
of free memory the database host has available.
116+
117+
180118
## Porting from SQLite
181119

182120
### Overview
@@ -185,9 +123,8 @@ The script `synapse_port_db` allows porting an existing synapse server
185123
backed by SQLite to using PostgreSQL. This is done in as a two phase
186124
process:
187125

188-
1. Copy the existing SQLite database to a separate location (while the
189-
server is down) and running the port script against that offline
190-
database.
126+
1. Copy the existing SQLite database to a separate location and run
127+
the port script against that offline database.
191128
2. Shut down the server. Rerun the port script to port any data that
192129
has come in since taking the first snapshot. Restart server against
193130
the PostgreSQL database.
@@ -245,3 +182,60 @@ PostgreSQL database configuration file `homeserver-postgres.yaml`:
245182
./synctl start
246183

247184
Synapse should now be running against PostgreSQL.
185+
186+
187+
## Troubleshooting
188+
189+
### Alternative auth methods
190+
191+
If you get an error along the lines of `FATAL: Ident authentication failed for
192+
user "synapse_user"`, you may need to use an authentication method other than
193+
`ident`:
194+
195+
* If the `synapse_user` user has a password, add the password to the `database:`
196+
section of `homeserver.yaml`. Then add the following to `pg_hba.conf`:
197+
198+
```
199+
host synapse synapse_user ::1/128 md5 # or `scram-sha-256` instead of `md5` if you use that
200+
```
201+
202+
* If the `synapse_user` user does not have a password, then a password doesn't
203+
have to be added to `homeserver.yaml`. But the following does need to be added
204+
to `pg_hba.conf`:
205+
206+
```
207+
host synapse synapse_user ::1/128 trust
208+
```
209+
210+
Note that line order matters in `pg_hba.conf`, so make sure that if you do add a
211+
new line, it is inserted before:
212+
213+
```
214+
host all all ::1/128 ident
215+
```
216+
217+
### Fixing incorrect `COLLATE` or `CTYPE`
218+
219+
Synapse will refuse to set up a new database if it has the wrong values of
220+
`COLLATE` and `CTYPE` set, and will log warnings on existing databases. Using
221+
different locales can cause issues if the locale library is updated from
222+
underneath the database, or if a different version of the locale is used on any
223+
replicas.
224+
225+
The safest way to fix the issue is to dump the database and recreate it with
226+
the correct locale parameter (as shown above). It is also possible to change the
227+
parameters on a live database and run a `REINDEX` on the entire database,
228+
however extreme care must be taken to avoid database corruption.
229+
230+
Note that the above may fail with an error about duplicate rows if corruption
231+
has already occurred, and such duplicate rows will need to be manually removed.
232+
233+
### Fixing inconsistent sequences error
234+
235+
Synapse uses Postgres sequences to generate IDs for various tables. A sequence
236+
and associated table can get out of sync if, for example, Synapse has been
237+
downgraded and then upgraded again.
238+
239+
To fix the issue shut down Synapse (including any and all workers) and run the
240+
SQL command included in the error message. Once done Synapse should start
241+
successfully.

0 commit comments

Comments
 (0)