dram.me

使用PostgreSQL作为Synapse数据存储

默认配置中,Synapse使用SQLite作为数据存储方案,但生产环境更倾向于用PostgreSQL,具体配置可以参考这里

首先需要在Synapse的virtualenv环境中利用pip安装psycopg2,再调整homeserver.yaml中的database段配置。

database:
  name: "psycopg2"
  args:
    user: "sa"
    password: "..."
    database: "synapse"
    host: "127.0.0.1"
    cp_min: 5
    cp_max: 10

PostgreSQL数据库的初始化和启动可以使用以下脚本:

#!/bin/sh

DB=synapse
HOST=localhost
USER=sa

export PGDATA="$(dirname "$(readlink -f "$0")")/data"

case $1 in
    init-cluster)
        pg_ctl init -o "--auth=password --pwprompt"
        cat >>"$PGDATA/postgresql.conf" <<EOF
listen_addresses = '$HOST'
logging_collector = on
max_prepared_transactions = 100
unix_socket_directories = '.'
EOF
        pg_ctl start
        ;;
    init-database)
        echo "Creating user ..."
        createuser -h $HOST -P $USER
        echo "Creating database ..."
        createdb -h $HOST -E UTF8 -O $USER $DB
        ;;
    shell)
        psql -h $HOST -U $USER $DB
        ;;
    start)
        pg_ctl start
        ;;
    stop)
        pg_ctl stop
        ;;
    clean)
        dropdb -h $HOST $DB
        createdb -h $HOST -E UTF8 -O $USER $DB
        ;;
    *)
        echo "Unknown command"
        ;;
esac