安装PostgreSQL

Debian 安装PostgreSQL

发行版安装

  • Debian 发行版包含了PostgreSQL,版本稍微低一些,例如当前PostgreSQL的current是17.2,而Debian 12(bookworm)则提供PostgreSQL 15:

通过 Debian 发行版安装PostgreSQL
apt install postgresql postgresql-contrib

postgresql-contrib 是社区捐赠软件包提供了有用的扩展和工具

  • 然后可以设置启动:

设置PostgreSQL自动启动
systemctl enable postgresql

使用官方软件仓库安装

PostgreSQL Apt仓库提供了更新的PostgreSQL版本以及补丁管理继承,可以自动完成所有PostgreSQL生命周期的所有版本更新: PostgreSQL Downlaad > Linux downloads (Debian) 提供了详细支持OS版本和架构,例如我在 树莓派Raspberry Pi 5 实际是 Debian bookworm(12.x)以及架构 arm64 都是官方支持的,所以通过以下方式安装:

自动仓库配置安装

简单执行以下命令就可以完成仓库配置:

自动完成仓库配置
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

手工配置仓库安装

执行以下命令配置Apt仓库:

手工完成仓库配置并安装
sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo apt update
sudo apt -y install postgresql

使用PostgreSQL的官方仓库进行安装的PostgreSQL是设置为自动启动服务,所以可以看到:

检查PostgreSQL
# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Wed 2024-12-11 12:50:50 CST; 4h 16min ago
   Main PID: 3940 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Dec 11 12:50:50 acloud-w3 systemd[1]: Starting postgresql.service - PostgreSQL RDBMS...
Dec 11 12:50:50 acloud-w3 systemd[1]: Finished postgresql.service - PostgreSQL RDBMS.

# ps aux | grep postgres
postgres    4765  0.0  0.3 223264 29696 ?        Ss   12:50   0:00 /usr/lib/postgresql/17/bin/postgres -D /var/lib/postgresql/17/main -c config_file=/etc/postgresql/17/main/postgresql.conf
postgres    4766  0.0  0.1 223392 10336 ?        Ss   12:50   0:00 postgres: 17/main: checkpointer
postgres    4767  0.0  0.0 223408  7264 ?        Ss   12:50   0:00 postgres: 17/main: background writer
postgres    4769  0.0  0.1 223264 10848 ?        Ss   12:50   0:00 postgres: 17/main: walwriter
postgres    4770  0.0  0.1 224864  9824 ?        Ss   12:50   0:00 postgres: 17/main: autovacuum launcher
postgres    4771  0.0  0.1 224864  8800 ?        Ss   12:50   0:00 postgres: 17/main: logical replication launcher
root        5836  0.0  0.0   6240  1536 pts/1    S+   17:09   0:00 grep postgres

macOS 安装PostgreSQL

第三方安装

Homebrew 安装

  • macOS 平台,最简单的还是使用 Homebrew 安装,不过,安装时需要指定安装主版本号,以下是安装最新的 17 系列:

使用 Homebrew 安装PostgreSQL
brew install postgresql@17

安装后的提示:

使用 Homebrew 安装PostgreSQL
This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgresql@17

When uninstalling, some dead symlinks are left behind so you may want to run:
  brew cleanup --prune-prefix

postgresql@17 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have postgresql@17 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/postgresql@17/bin:$PATH"' >> ~/.zshrc

For compilers to find postgresql@17 you may need to set:
  export LDFLAGS="-L/usr/local/opt/postgresql@17/lib"
  export CPPFLAGS="-I/usr/local/opt/postgresql@17/include"

To start postgresql@17 now and restart at login:
  brew services start postgresql@17
Or, if you don't want/need a background service you can just run:
  LC_ALL="C" /usr/local/opt/postgresql@17/bin/postgres -D /usr/local/var/postgresql@17
  • 根据提示,在 ~/.zshrc 中添加如下内容:

~/.zshrc 中添加PostgreSQL相关配置
export PATH="/usr/local/opt/postgresql@17/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/postgresql@17/lib"
export CPPFLAGS="-I/usr/local/opt/postgresql@17/include"
  • 启动有两种方式

使用 brew 服务启动:

使用 brew 服务启动 PostgreSQL
brew services start postgresql@17

或者命令行前台启动:

使用命令启动 PostgreSQL
LC_ALL="C" /usr/local/opt/postgresql@17/bin/postgres -D /usr/local/var/postgresql@17
  • 创建 postgres 用户( pgAdmin 使用这个角色访问):

通过 Homebrew 安装的PostgreSQL,需要创建一个 postgres 系统用户角色
/usr/local/opt/postgresql@17/bin/createuser -s postgres

下一步

完成PostgreSQL之后,就可以 访问Postgresql (包括设置权限)

参考