在Ubuntu22.04上安装Postgresql

切换到清华源

最初使用的是阿里云的源,apt 找不到 libllvm 包。

写入 /etc/apt/sources.list

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu jammy main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu jammy-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu jammy universe
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy universe
deb http://cn.archive.ubuntu.com/ubuntu jammy-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://cn.archive.ubuntu.com/ubuntu jammy multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy multiverse
deb http://cn.archive.ubuntu.com/ubuntu jammy-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse

deb http://cn.archive.ubuntu.com/ubuntu jammy-security main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu jammy-security universe
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy-security universe
deb http://cn.archive.ubuntu.com/ubuntu jammy-security multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu jammy-security multiverse

安装 libllvm14

执行:

sudo apt update
sudo apt install postgresql postgresql-contrib

提示 libllvm14 包找不到,先安装改包:

sudo apt install libllvm14

安装 ssl-cert-snakeoil.pem

再执行安装命令,成功安装,但是启动失败:

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2023-03-31 15:42:45 UTC; 2s ago
    Process: 9793 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 9793 (code=exited, status=0/SUCCESS)
        CPU: 3ms

Mar 31 15:42:45 xingzii-vm systemd[1]: Starting PostgreSQL RDBMS...
Mar 31 15:42:45 xingzii-vm systemd[1]: Finished PostgreSQL RDBMS.

到 /var/log/postgresql 查看日志,提示:

2023-03-31 15:37:02.316 UTC [9453] FATAL:  could not load server certificate file "/etc/ssl/certs/ssl-cert-snakeoil.pem": SSL error code 2147483661
2023-03-31 15:37:02.316 UTC [9453] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.

网上看了一下,安装这个就好:

sudo apt install ssl-cert && sudo make-ssl-cert generate-default-snakeoil

安装好记得修改一下 /etc/ssl/certs/ssl-cert-snakeoil.pem 的权限:

sudo chmod 755 /etc/ssl/certs/ssl-cert-snakeoil.pem

再启动 postgresql 就好了。

验证:

sudo -u postgres psql -c "SELECT version();"
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LC_TERMINAL = "iTerm2",
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
                                                                version
----------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.14 (Ubuntu 12.14-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)