文章

ubuntu部署postgresql+timescaladb时序数据库

ubuntu部署postgresql+timescaladb时序数据库

ubuntu部署postgresql+timescaladb时序数据库

中间件版本

  • postgresql-14=14.15-0ubuntu0.22.04.1
  • timescaledb-2-postgresql-14=2.17.2~ubuntu22.04

pg数据库安装

1
2
3
4
sudo apt install -y postgresql-14=14.15-0ubuntu0.22.04.1
sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo systemctl status postgresql

pg数据库配置

pg数据库配置文件一般位于/etc/postgresql/14/main/postgresql.conf,修改以下内容。其他按需修改。

1
2
## 默认timezone = 'Etc/UTC'
timezone = 'Asia/Shanghai'

配置允许远程连接

1
2
echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/14/main/pg_hba.conf
echo "listen_addresses = '*'" >> /etc/postgresql/14/main/postgresql.conf

修改后重启服务systemctl restart postgresql

timescaledb扩展安装

参考,注意版本和pg数据库版本一致。

配置APT存储库

1
2
3
4
5
6
## 添加官方的APT存储库,到默认的Ubuntu存储库中。先导入存储库的GPG密钥
wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | sudo apt-key add -
echo "deb https://packagecloud.io/timescale/timescaledb/ubuntu/ $(lsb_release -c -s) main" | sudo tee /etc/apt/sources.list.d/timescaledb.list
sudo apt update
sudo apt install -y timescaledb-2-postgresql-14=2.17.2~ubuntu22.04
sudo apt install -y timescaledb-2-loader-postgresql-14='2.17.2~ubuntu22.04'

配置扩展

修改postgresql.conf文件的shared_preload_libraries配置项,添加值timescaledb

1
2
## 注意如果原来有使用其他扩展,则在末尾追加,逗号分隔
echo "shared_preload_libraries = 'timescaledb'" >> /etc/postgresql/14/main/postgresql.conf

修改后,执行systemctl restart postgresql重启数据库。

扩展使用

1
2
-- 安装扩展。后续可以使用timescaledb特性
CREATE EXTENSION IF NOT EXISTS timescaledb;
本文由作者按照 CC BY 4.0 进行授权