Rocky Linux 8 に PostgreSQL 14 + TimescaleDB をインストールしてみた

2022.12.05

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、大前です。

Red Hat Enterprise Linux 互換の OS である Rocky Linux を EC2 で構築し、PostgreSQL の拡張として実装されている時系列データベースの TimescaleDB のインストールを試す機会がありましたのでブログに手順などを残したいと思います。

Rocky Linux や TimescaleDB の詳細については、それぞれ下記ブログで言及されていますので、それぞれご参照ください。

今回は Rocky Linux 8 + PostgreSQL 14 で試しました。

やってみた

Rocky Linux の立ち上げ

今回は AWS Marketplace AMI として提供されている、Rocky Linux 8 の AMI を指定して EC2 インスタンスを立ち上げます。


また、EC2 に対してセッションマネージャーで接続したいのですが、Rocky Linux は 2022/12 時点で SSM Agent がインストールされていないため、ユーザーデータで SSM Agent をインストールして起動するコマンドを指定しておきます。

#!/bin/bash
sudo dnf install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
sudo systemctl enable amazon-ssm-agent
sudo systemctl start amazon-ssm-agent

参考 : Rocky Linux インスタンスに SSM Agent を手動でインストールする - AWS Systems Manager


その他、セッションマネージャーを利用するための前提条件を満たす様に、ネットワークや IAM ロールなどを設定し、EC2 インスタンスを立ち上げます。

参考 : ステップ 1: Session Manager の前提条件を満たす - AWS Systems Manager


TimescaleDB のインストール

Rocky Linux が無事起動し、セッションマネージャーで接続ができたら TimescaleDB をインストールしていきます。インストール手順は下記の公式ドキュメントに従って進めました。

参考 : Timescale Documentation | Install TimescaleDB on Red Hat and CentOS


リポジトリを追加し、定義ファイルを用意します。

# リポジトリを追加
sudo yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{centos})-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
# リポジトリの定義ファイルを作成
sudo tee /etc/yum.repos.d/timescale_timescaledb.repo <<EOL
[timescale_timescaledb]
name=timescale_timescaledb
baseurl=https://packagecloud.io/timescale/timescaledb/el/$(rpm -E %{rhel})/\$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/timescale/timescaledb/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
EOL


yum update をかけておきます。

# yum update
sudo yum update -y


PostgreSQL モジュールの無効化を行います。公式ドキュメントでは「CentOS 8 もしくは Red Hat Enterprise Linux 8 の場合必要」と記載されていますが、Rocky Linux 8 においてもこの手順を実施しないと後続のコマンドでエラーが発生しました。

# PostgreSQL モジュールの無効化
sudo dnf -qy module disable postgresql


下記のコマンドによって、TimescaleDB と PostgreSQL がインストールされます。

# TimescaleDB のインストール
sudo yum install timescaledb-2-postgresql-14 -y
Last metadata expiration check: 0:00:44 ago on Mon 05 Dec 2022 08:28:20 AM UTC.
Dependencies resolved.
=============================================================================================================================================================
 Package                                             Architecture            Version                            Repository                              Size
=============================================================================================================================================================
Installing:
 timescaledb-2-postgresql-14                         x86_64                  2.8.1-0.el8                        timescale_timescaledb                  608 k
Installing dependencies:
 libicu                                              x86_64                  60.3-2.el8_1                       baseos                                 8.8 M
 lz4                                                 x86_64                  1.8.3-3.el8_4                      baseos                                 102 k
 postgresql14                                        x86_64                  14.6-1PGDG.rhel8                   pgdg14                                 1.5 M
 postgresql14-libs                                   x86_64                  14.6-1PGDG.rhel8                   pgdg14                                 278 k
 postgresql14-server                                 x86_64                  14.6-1PGDG.rhel8                   pgdg14                                 5.7 M
 timescaledb-2-loader-postgresql-14                  x86_64                  2.8.1-0.el8                        timescale_timescaledb                   27 k
 timescaledb-tools                                   x86_64                  0.14.2-0.el8                       timescale_timescaledb                  2.8 M

Transaction Summary
=============================================================================================================================================================
Install  8 Packages

(中略)

Installed:
  libicu-60.3-2.el8_1.x86_64                        lz4-1.8.3-3.el8_4.x86_64                       postgresql14-14.6-1PGDG.rhel8.x86_64
  postgresql14-libs-14.6-1PGDG.rhel8.x86_64         postgresql14-server-14.6-1PGDG.rhel8.x86_64    timescaledb-2-loader-postgresql-14-2.8.1-0.el8.x86_64
  timescaledb-2-postgresql-14-2.8.1-0.el8.x86_64    timescaledb-tools-0.14.2-0.el8.x86_64

Complete!


<br>

データベースの初期化を実施したら、TimescaleDB のインストールまで完了です。


# データベースの初期化
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb


続いて、TimescaleDB を PostgreSQL にて有効化するために PostgreSQL を起動します。

# PostgreSQL のサービスを起動
systemctl enable postgresql-14
systemctl start postgresql-14


PostgreSQL に接続するためのパスワード設定を行います。

# postgres ユーザーで PostgreSQL に接続
sh-4.4$ sudo -u postgres psql

# パスワード設定
postgres=# \password postgres
Enter new password for user "postgres":
Enter it again:

# exit
postgres=# \q


公式ドキュメントの手順には記載されていませんが、設定ファイルにて shared_preload_libraries を指定しないと拡張機能の有効化ができませんでしたので、ここで設定ファイルの追記を実行しておきます。

# shared_preload_libraries に timescaledb を指定するように設定ファイルを追記
echo "shared_preload_libraries = 'timescaledb'" | sudo tee -a /var/lib/pgsql/14/data/postgresql.conf

# サービス再起動
sudo systemctl restart postgresql-14


再度 PostgreSQL に接続し、テスト用の Database を用意して接続します。

# 再度接続
sh-4.4$ psql -U postgres -h localhost
Password for user postgres:
psql (14.6)
Type "help" for help.

# Database 作成
postgres=# CREATE database tsdb;
CREATE DATABASE

# 作成した Database に接続
postgres=# \c tsdb
You are now connected to database "tsdb" as user "postgres".
tsdb=#


TimescaleDB を拡張機能として追加します。晴れて Rocky Linux 上で TimescaleDB が利用できる様になりました。

# 拡張機能の追加
tsdb=# CREATE EXTENSION IF NOT EXISTS timescaledb;
WARNING:
WELCOME TO
 _____ _                               _     ____________
|_   _(_)                             | |    |  _  \ ___ \
  | |  _ _ __ ___   ___  ___  ___ __ _| | ___| | | | |_/ /
  | | | |  _ ` _ \ / _ \/ __|/ __/ _` | |/ _ \ | | | ___ \
  | | | | | | | | |  __/\__ \ (_| (_| | |  __/ |/ /| |_/ /
  |_| |_|_| |_| |_|\___||___/\___\__,_|_|\___|___/ \____/
               Running version 2.8.1
For more information on TimescaleDB, please visit the following links:

 1. Getting started: https://docs.timescale.com/timescaledb/latest/getting-started
 2. API reference documentation: https://docs.timescale.com/api/latest
 3. How TimescaleDB is designed: https://docs.timescale.com/timescaledb/latest/overview/core-concepts

Note: TimescaleDB collects anonymous reports to better understand and assist our users.
For more information and how to disable, please see our docs https://docs.timescale.com/timescaledb/latest/how-to-guides/configuration/telemetry.

CREATE EXTENSION
tsdb=#

# 拡張機能が追加されていることを確認
tsdb=# \dx
                                      List of installed extensions
    Name     | Version |   Schema   |                            Description
-------------+---------+------------+-------------------------------------------------------------------
 plpgsql     | 1.0     | pg_catalog | PL/pgSQL procedural language
 timescaledb | 2.8.1   | public     | Enables scalable inserts and complex queries for time-series data
(2 rows)

(おまけ) Amazon Linux 2 では同様の手順をやってみた

Install self-hosted TimescaleDB on Red Hat-based systems で記載されている対応ディストリビューションには Amazon Linux 2 がなかったのですが、同じ RHEL 系だしインストールできるのでは?と思い試してみましたが、TimescaleDB をインストールする部分(sudo yum install timescaledb-2-postgresql-14 -y)にて依存関係に起因する旨のメッセージと共にエラーが発生し、失敗しました。

上記を解決するには、上記手順でも実施した PostgreSQL モジュールを無効化するコマンド(sudo dnf -qy module disable postgresql)の実行が必要なのですが、Amazon Linux 2 では dnf コマンドを利用できないため、今回の手順では Amazon Linux 2 に TimescaleDB をインストールすることは難しいことがわかりました。(Amazon Linux 2 上で dnf コマンドを利用する方法などがあるのかもしれませんが、浅学な自分ではわかりませんでした、、、)

現在プレビュー版で発表されている Amazon Linux 2022 では yum が dnf に置き換わることがわかっているため、Amazon Linux 2022 が正式にリリースされたら、Amazon Linux 上でも TimescaleDB が手軽に利用できる事が期待できます。

参考 : Package 管理ツール - Amazon Linux 2022


一応、ソースからビルドする方法も提供されているため、どうしても今回の手順に対応していないディストリビューション上で TimescaleDB を動かしたい場合はトライしてみると良いかもしれません。(Amazon Linux 2 上で軽く試してみましたが、長い戦いになりそうな予感がしたので今回は諦めました)

参考 : Install TimescaleDB from source

おわりに

Red Hat Enterprise Linux 互換の OS である Rocky Linux を EC2 で構築し、PostgreSQL の拡張として実装されている時系列データベースの TimescaleDB のインストールを試してみました。

2022/12 時点では RDS for PostgreSQL で利用可能な拡張機能として TimescaleDB がサポートされていないため、今回の様に EC2 上で PostgreSQL + TimescaleDB 環境を用意する必要があるケースも出てくるかと思います。

Extension versions for Amazon RDS for PostgreSQL - Amazon Relational Database Service

以上、AWS 事業本部の大前でした。

参考