2021-08-20 22:54:08 +02:00
|
|
|
# PostgreSQL database details
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2021-04-27 01:11:02 +02:00
|
|
|
Starting with Zulip 3.0, Zulip supports a range of PostgreSQL
|
2023-12-21 21:46:51 +01:00
|
|
|
versions. PostgreSQL 16 is the current default for new installations;
|
|
|
|
PostgreSQL 12, 13, 14, 15, and 16 are all supported.
|
2020-06-27 01:26:57 +02:00
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
Previous versions of Zulip used whatever version of PostgreSQL was
|
2024-03-25 22:31:39 +01:00
|
|
|
included with the base operating system (E.g. PostgreSQL 14 on Ubuntu
|
|
|
|
22.04). We recommend that installations currently using older
|
2023-05-08 17:51:25 +02:00
|
|
|
PostgreSQL releases [upgrade to PostgreSQL 15][upgrade-postgresql], as
|
2021-11-08 22:12:57 +01:00
|
|
|
we may drop support for older PostgreSQL in a future release.
|
2020-06-27 01:26:57 +02:00
|
|
|
|
2023-01-17 04:33:42 +01:00
|
|
|
[upgrade-postgresql]: upgrade.md#upgrading-postgresql
|
2020-06-27 01:26:57 +02:00
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
#### Remote PostgreSQL database
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2020-08-11 01:47:54 +02:00
|
|
|
This is a bit annoying to set up, but you can configure Zulip to use a
|
2020-10-26 22:27:53 +01:00
|
|
|
dedicated PostgreSQL server by setting the `REMOTE_POSTGRES_HOST`
|
|
|
|
variable in /etc/zulip/settings.py, and configuring PostgreSQL
|
2016-07-12 23:37:39 +02:00
|
|
|
certificate authentication (see
|
|
|
|
http://www.postgresql.org/docs/9.1/static/ssl-tcp.html and
|
|
|
|
http://www.postgresql.org/docs/9.1/static/libpq-ssl.html for
|
|
|
|
documentation on how to set this up and deploy the certificates) to
|
2020-08-19 21:55:28 +02:00
|
|
|
make the DATABASES configuration in `zproject/computed_settings.py`
|
|
|
|
work (or override that configuration).
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
If you want to use a remote PostgreSQL database, you should configure
|
2016-07-12 23:37:39 +02:00
|
|
|
the information about the connection with the server. You need a user
|
|
|
|
called "zulip" in your database server. You can configure these
|
2018-05-22 20:29:33 +02:00
|
|
|
options in `/etc/zulip/settings.py` (the below descriptions are from the
|
2020-10-23 02:43:28 +02:00
|
|
|
PostgreSQL documentation):
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- `REMOTE_POSTGRES_HOST`: Name or IP address of the remote host
|
|
|
|
- `REMOTE_POSTGRES_SSLMODE`: SSL Mode used to connect to the server,
|
2017-01-05 23:23:16 +01:00
|
|
|
different options you can use are:
|
2021-08-20 21:45:39 +02:00
|
|
|
- disable: I don't care about security, and I don't want to pay the
|
2017-01-05 23:23:16 +01:00
|
|
|
overhead of encryption.
|
2021-08-20 21:45:39 +02:00
|
|
|
- allow: I don't care about security, but I will pay the overhead of
|
2017-01-05 23:23:16 +01:00
|
|
|
encryption if the server insists on it.
|
2021-08-20 21:45:39 +02:00
|
|
|
- prefer: I don't care about encryption, but I wish to pay the
|
2017-01-05 23:23:16 +01:00
|
|
|
overhead of encryption if the server supports it.
|
2021-08-20 21:45:39 +02:00
|
|
|
- require: I want my data to be encrypted, and I accept the
|
2017-01-05 23:23:16 +01:00
|
|
|
overhead. I trust that the network will make sure I always connect
|
|
|
|
to the server I want.
|
2021-08-20 21:45:39 +02:00
|
|
|
- verify-ca: I want my data encrypted, and I accept the overhead. I
|
2017-01-05 23:23:16 +01:00
|
|
|
want to be sure that I connect to a server that I trust.
|
2021-08-20 21:45:39 +02:00
|
|
|
- verify-full: I want my data encrypted, and I accept the
|
2017-01-05 23:23:16 +01:00
|
|
|
overhead. I want to be sure that I connect to a server I trust,
|
|
|
|
and that it's the one I specify.
|
|
|
|
|
2024-03-20 21:22:11 +01:00
|
|
|
Set the remote server's PostgreSQL version in `/etc/zulip/zulip.conf`:
|
|
|
|
|
|
|
|
```ini
|
|
|
|
[postgresql]
|
|
|
|
# Set this to match the version running on your remote PostgreSQL server
|
|
|
|
version = 16
|
|
|
|
```
|
|
|
|
|
2017-01-05 23:23:16 +01:00
|
|
|
Then you should specify the password of the user zulip for the
|
|
|
|
database in /etc/zulip/zulip-secrets.conf:
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```ini
|
2016-07-12 23:37:39 +02:00
|
|
|
postgres_password = xxxx
|
|
|
|
```
|
|
|
|
|
|
|
|
Finally, you can stop your database on the Zulip server via:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
2016-07-12 23:37:39 +02:00
|
|
|
sudo service postgresql stop
|
|
|
|
sudo update-rc.d postgresql disable
|
|
|
|
```
|
|
|
|
|
|
|
|
In future versions of this feature, we'd like to implement and
|
2020-10-26 22:27:53 +01:00
|
|
|
document how to the remote PostgreSQL database server itself
|
2016-07-12 23:37:39 +02:00
|
|
|
automatically by using the Zulip install script with a different set
|
2020-10-23 02:43:28 +02:00
|
|
|
of Puppet manifests than the all-in-one feature; if you're interested
|
2016-07-12 23:37:39 +02:00
|
|
|
in working on this, post to the Zulip development mailing list and we
|
|
|
|
can give you some tips.
|
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
#### Debugging PostgreSQL database issues
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
When debugging PostgreSQL issues, in addition to the standard `pg_top`
|
2016-07-12 23:37:39 +02:00
|
|
|
tool, often it can be useful to use this query:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```postgresql
|
2016-07-12 23:37:39 +02:00
|
|
|
SELECT procpid,waiting,query_start,current_query FROM pg_stat_activity ORDER BY procpid;
|
|
|
|
```
|
|
|
|
|
|
|
|
which shows the currently running backends and their activity. This is
|
|
|
|
similar to the pg_top output, with the added advantage of showing the
|
|
|
|
complete query, which can be valuable in debugging.
|
|
|
|
|
2021-09-08 00:23:24 +02:00
|
|
|
To stop a runaway query, you can run
|
|
|
|
`SELECT pg_cancel_backend(pid int)` or
|
|
|
|
`SELECT pg_terminate_backend(pid int)` as the 'postgres' user. The
|
|
|
|
former cancels the backend's current query and the latter terminates
|
|
|
|
the backend process. They are implemented by sending SIGINT and
|
2021-08-20 21:53:28 +02:00
|
|
|
SIGTERM to the processes, respectively. We recommend against sending
|
2021-09-08 00:23:24 +02:00
|
|
|
a PostgreSQL process SIGKILL. Doing so will cause the database to kill
|
|
|
|
all current connections, roll back any pending transactions, and enter
|
|
|
|
recovery mode.
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
#### Stopping the Zulip PostgreSQL database
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
To start or stop PostgreSQL manually, use the pg_ctlcluster command:
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
2016-07-12 23:37:39 +02:00
|
|
|
pg_ctlcluster 9.1 [--force] main {start|stop|restart|reload}
|
|
|
|
```
|
|
|
|
|
|
|
|
By default, using stop uses "smart" mode, which waits for all clients
|
|
|
|
to disconnect before shutting down the database. This can take
|
|
|
|
prohibitively long. If you use the --force option with stop,
|
|
|
|
pg_ctlcluster will try to use the "fast" mode for shutting
|
|
|
|
down. "Fast" mode is described by the manpage thusly:
|
|
|
|
|
2021-08-20 22:19:05 +02:00
|
|
|
> With the --force option the "fast" mode is used which rolls back all
|
|
|
|
> active transactions, disconnects clients immediately and thus shuts
|
|
|
|
> down cleanly. If that does not work, shutdown is attempted again in
|
|
|
|
> "immediate" mode, which can leave the cluster in an inconsistent state
|
|
|
|
> and thus will lead to a recovery run at the next start. If this still
|
|
|
|
> does not help, the postmaster process is killed. Exits with 0 on
|
|
|
|
> success, with 2 if the server is not running, and with 1 on other
|
|
|
|
> failure conditions. This mode should only be used when the machine is
|
|
|
|
> about to be shut down.
|
2016-07-12 23:37:39 +02:00
|
|
|
|
|
|
|
Many database parameters can be adjusted while the database is
|
|
|
|
running. Just modify /etc/postgresql/9.1/main/postgresql.conf and
|
|
|
|
issue a reload. The logs will note the change.
|
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
#### Debugging issues starting PostgreSQL
|
2016-07-12 23:37:39 +02:00
|
|
|
|
|
|
|
pg_ctlcluster often doesn't give you any information on why the
|
|
|
|
database failed to start. It may tell you to check the logs, but you
|
|
|
|
won't find any information there. pg_ctlcluster runs the following
|
2020-10-26 22:27:53 +01:00
|
|
|
command underneath when it actually goes to start PostgreSQL:
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
2017-01-06 00:06:34 +01:00
|
|
|
/usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -s -o \
|
|
|
|
'-c config_file="/etc/postgresql/9.1/main/postgresql.conf"'
|
2016-07-12 23:37:39 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Since pg_ctl doesn't redirect stdout or stderr, running the above can
|
|
|
|
give you better diagnostic information. However, you might want to
|
2020-10-26 22:27:53 +01:00
|
|
|
stop PostgreSQL and restart it using pg_ctlcluster after you've debugged
|
2016-07-12 23:37:39 +02:00
|
|
|
with this approach, since it does bypass some of the work that
|
|
|
|
pg_ctlcluster does.
|
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
#### PostgreSQL vacuuming alerts
|
2016-07-12 23:37:39 +02:00
|
|
|
|
2020-10-26 22:27:53 +01:00
|
|
|
The `autovac_freeze` PostgreSQL alert from `check_postgres` is
|
2021-08-20 21:53:28 +02:00
|
|
|
particularly important. This alert indicates that the age (in terms
|
2016-07-12 23:37:39 +02:00
|
|
|
of number of transactions) of the oldest transaction id (XID) is
|
2021-08-20 21:53:28 +02:00
|
|
|
getting close to the `autovacuum_freeze_max_age` setting. When the
|
2020-10-26 22:27:53 +01:00
|
|
|
oldest XID hits that age, PostgreSQL will force a VACUUM operation,
|
2016-07-12 23:37:39 +02:00
|
|
|
which can often lead to sudden downtime until the operation finishes.
|
|
|
|
If it did not do this and the age of the oldest XID reached 2 billion,
|
|
|
|
transaction id wraparound would occur and there would be data loss.
|
|
|
|
To clear the nagios alert, perform a `VACUUM` in each indicated
|
|
|
|
database as a database superuser (`postgres`).
|
|
|
|
|
|
|
|
See
|
|
|
|
http://www.postgresql.org/docs/9.1/static/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND
|
2020-10-26 22:27:53 +01:00
|
|
|
for more details on PostgreSQL vacuuming.
|