下面是在 Ubuntu 22.04 x64 系统上为 PostgreSQL 14 安装 TimescaleDB 并激活它的步骤指南。


在 Ubuntu 22.04 上为 PostgreSQL 14 安装 TimescaleDB 指南

前提条件:

步骤 1: 安装 PostgreSQL 14 (如果尚未安装)

TimescaleDB 是 PostgreSQL 的一个扩展,因此你需要先安装 PostgreSQL 14。

  1. 更新你的包列表:

    sudo apt update
    
  2. 安装 PostgreSQL 14 服务器和客户端:

    sudo apt install postgresql-14 postgresql-client-14
    
  3. (可选) 检查 PostgreSQL 服务状态,确保它正在运行:

    sudo systemctl status [email protected]
    # 或者使用通用的状态命令
    # sudo systemctl status postgresql
    

    如果服务没有运行,可以启动它:

    sudo systemctl start [email protected]
    

步骤 2: 添加 TimescaleDB APT 软件源

为了安装最新稳定版的 TimescaleDB,你需要添加 TimescaleDB 官方的 APT 软件源。

  1. (如果需要) 安装必要的工具:

    sudo apt install -y wget gnupg pgdg-keyring
    
  2. 添加 TimescaleDB 的 PGP 密钥:

    # 下载并导入 GPG 密钥
    wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/timescaledb.gpg
    
  3. 添加 TimescaleDB 软件源配置:

    # 创建源列表文件,$(lsb_release -cs) 会自动替换为你的 Ubuntu 版本代号 (例如 jammy)
    sudo sh -c "echo 'deb [signed-by=/usr/share/keyrings/timescaledb.gpg] https://packagecloud.io/timescale/timescaledb/ubuntu/ $(lsb_release -cs) main' > /etc/apt/sources.list.d/timescaledb.list"
    
  4. 刷新 APT 包列表以包含新的软件源:

    sudo apt update
    

步骤 3: 安装 TimescaleDB 扩展

现在你可以安装与 PostgreSQL 14 兼容的 TimescaleDB 扩展包了。

# 安装 TimescaleDB for PostgreSQL 14 (通常包名包含 TimescaleDB 版本号和 PG 版本号)
# timescaledb-2-* 通常指 TimescaleDB 2.x 版本系列
sudo apt install timescaledb-2-postgresql-14

在安装过程中,APT 会自动处理依赖关系。

步骤 4: 配置 PostgreSQL (使用 timescaledb-tune)

安装完成后,运行 timescaledb-tune 工具来优化你的 PostgreSQL 配置,使其更好地适应 TimescaleDB。这个工具会修改 postgresql.conf 文件。

  1. 运行 timescaledb-tune。你需要指定 pg_config 的路径和 PostgreSQL 主配置文件的路径。对于标准的 Ubuntu 安装,路径通常如下:

    sudo timescaledb-tune --pg-config=/usr/bin/pg_config --conf-path=/etc/postgresql/14/main/postgresql.conf
    
  2. 该工具会分析你的系统资源并建议修改 PostgreSQL 的配置参数(如 shared_buffers, work_mem 等)。它还会确保 timescaledb 被添加到 shared_preload_libraries 中,这是 TimescaleDB 正常工作所必需的。

  3. 仔细阅读建议的更改,如果同意,输入 yes 并按 Enter 确认。

步骤 5: 重启 PostgreSQL 服务

为了让 postgresql.conf 中的更改生效(特别是 shared_preload_libraries 的设置),你需要重启 PostgreSQL 服务。

sudo systemctl restart [email protected]
# 或者使用通用的重启命令
# sudo systemctl restart postgresql

步骤 6: 在数据库中激活 TimescaleDB 扩展

TimescaleDB 扩展需要在你想要使用它的 每个 数据库中单独激活。

  1. 使用 psql 工具连接到你想要启用 TimescaleDB 的目标数据库。你需要指定数据库名和用户名。如果你想在 postgres 用户的默认数据库 postgres 中启用它,可以这样做:

    # 以 postgres 系统用户身份连接到 postgres 数据库
    sudo -u postgres psql -d postgres
    

    如果你有自己的数据库(例如 mydb)和用户(例如 myuser),则使用:

    psql -U myuser -d mydb -h localhost
    

    (如果连接失败,请检查你的 PostgreSQL 认证设置 (pg_hba.conf))

  2. psql 提示符下,运行以下 SQL 命令来创建并激活 TimescaleDB 扩展:

    CREATE EXTENSION IF NOT EXISTS timescaledb;
    

    IF NOT EXISTS 确保如果扩展已经存在,命令不会报错。

  3. (可选) 验证扩展是否已加载。在 psql 中运行:

    \dx
    

    你应该能在输出列表中看到 timescaledb,并显示其版本号。

  4. 退出 psql

    \q
    

完成!

现在 TimescaleDB 已经成功安装在你的 PostgreSQL 14 实例上,并且在你选择的数据库中激活了。你可以开始创建 Hypertables 并利用 TimescaleDB 强大的时序数据功能了。

重要提示:

希望这个指南对你有帮助!