分布式 PostgreSQL 集群(Citus)官方安裝指南
單節(jié)點(diǎn) Citus
Docker (Mac 與 Linux)
Docker 鏡像僅用于開發(fā)/測(cè)試目的, 并且尚未準(zhǔn)備好用于生產(chǎn)用途。
您可以使用一個(gè)命令在 Docker 中啟動(dòng) Citus:
# start the image
docker run -d --name citus -p 5432:5432 -e POSTGRES_PASSWORD=mypass \
citusdata/citus:10.2
# verify it's running, and that Citus is installed:
psql -U postgres -h localhost -d postgres -c "SELECT * FROM citus_version();"
您應(yīng)該會(huì)看到最新版本的 Citus。
集群?jiǎn)?dòng)并運(yùn)行后,您可以訪問我們關(guān)于多租戶應(yīng)用程序或?qū)崟r(shí)分析的教程,在幾分鐘內(nèi)開始使用 Citus。
- https://docs.citusdata.com/en/v10.2/get_started/tutorial_multi_tenant.html#multi-tenant-tutorial。
- https://docs.citusdata.com/en/v10.2/get_started/tutorial_realtime_analytics.html#real-time-analytics-tutorial。
如果您的機(jī)器上已經(jīng)運(yùn)行了 PostgreSQL,則在啟動(dòng) Docker 容器時(shí)可能會(huì)遇到此錯(cuò)誤:
Error starting userland proxy:
Bind for 0.0.0.0:5432: unexpected error address already in use
這是因?yàn)?Citus 鏡像嘗試綁定到標(biāo)準(zhǔn) PostgreSQL 端口 5432。要解決此問題,請(qǐng)使用 -p 選項(xiàng)選擇不同的端口。您還需要在下面的 psql 命令中使用新端口。
Ubuntu 或 Debian
本節(jié)介紹在您自己的 Linux 機(jī)器上使用 deb 包設(shè)置單節(jié)點(diǎn) Citus 集群所需的步驟。
1、安裝 PostgreSQL 14 和 Citus 擴(kuò)展
# Add Citus repository for package manager
curl https://install.citusdata.com/community/deb.sh | sudo bash
# install the server and initialize db
sudo apt-get -y install postgresql-14-citus-10.2
2、初始化集群
讓我們?cè)诖疟P上創(chuàng)建一個(gè)新數(shù)據(jù)庫。為了方便使用 PostgreSQL Unix domain socket 連接,我們將使用 postgres 用戶。
# this user has access to sockets in /var/run/postgresql
sudo su - postgres
# include path to postgres binaries
export PATH=$PATH:/usr/lib/postgresql/14/bin
cd ~
mkdir citus
initdb -D citus
Citus 是一個(gè) Postgres 擴(kuò)展。要告訴 Postgres 使用這個(gè)擴(kuò)展,你需要將它添加到一個(gè)名為 shared_preload_libraries 的配置變量中:
echo "shared_preload_libraries = 'citus'" >> citus/postgresql.conf
3、啟動(dòng)數(shù)據(jù)庫服務(wù)器
最后,我們將為新目錄啟動(dòng)一個(gè) PostgreSQL 實(shí)例:
pg_ctl -D citus -o "-p 9700" -l citus_logfile start
上面您將 Citus 添加到 shared_preload_libraries。這讓它可以連接到 Postgres 的一些深層部分,交換查詢計(jì)劃器(query planner)和執(zhí)行器(executor)。在這里,我們加載 Citus 面向用戶的一面(例如您將很快調(diào)用的函數(shù)):
psql -p 9700 -c "CREATE EXTENSION citus;"
4、驗(yàn)證安裝是否成功
要驗(yàn)證安裝是否成功,并且 Citus 已安裝:
psql -p 9700 -c "select citus_version();"
您應(yīng)該看到 Citus 擴(kuò)展的詳細(xì)信息。
Fedora, CentOS, 或 Red Hat
本節(jié)介紹在您自己的 Linux 機(jī)器上使用 RPM 包設(shè)置單節(jié)點(diǎn) Citus 集群所需的步驟。
1、安裝 PostgreSQL 14 和 Citus 擴(kuò)展
# Add Citus repository for package manager
curl https://install.citusdata.com/community/rpm.sh | sudo bash
# install Citus extension
sudo yum install -y citus102_14
2、初始化集群
讓我們?cè)诖疟P上創(chuàng)建一個(gè)新數(shù)據(jù)庫。為了方便使用 PostgreSQL Unix domain socket 連接,我們將使用 postgres 用戶。
# this user has access to sockets in /var/run/postgresql
sudo su - postgres
# include path to postgres binaries
export PATH=$PATH:/usr/pgsql-14/bin
cd ~
mkdir citus
initdb -D citus
Citus 是一個(gè) Postgres 擴(kuò)展。要告訴 Postgres 使用這個(gè)擴(kuò)展,你需要將它添加到一個(gè)名為 shared_preload_libraries 的配置變量中:
echo "shared_preload_libraries = 'citus'" >> citus/postgresql.conf
3、啟動(dòng)數(shù)據(jù)庫服務(wù)器
最后,我們將為新目錄啟動(dòng)一個(gè) PostgreSQL 實(shí)例:
pg_ctl -D citus -o "-p 9700" -l citus_logfile start
上面您將 Citus 添加到 shared_preload_libraries。這讓它可以連接到 Postgres 的一些深層部分,交換查詢計(jì)劃器(query planner)和執(zhí)行器(executor)。在這里,我們加載 Citus 面向用戶的一面(例如您將很快調(diào)用的函數(shù)):
psql -p 9700 -c "CREATE EXTENSION citus;"
4、驗(yàn)證安裝是否成功
要驗(yàn)證安裝是否成功,并且 Citus 已安裝:
psql -p 9700 -c "select citus_version();"
您應(yīng)該看到 Citus 擴(kuò)展的詳細(xì)信息。
在此步驟中,您已完成安裝過程并準(zhǔn)備好使用 Citus 集群。為了幫助您入門,我們提供了一個(gè)教程,其中包含有關(guān)在幾分鐘內(nèi)使用示例數(shù)據(jù)設(shè)置 Citus 集群的說明。
多節(jié)點(diǎn) Citus
Ubuntu 或 Debian
本節(jié)介紹使用 deb 包在您自己的 Linux 機(jī)器上設(shè)置多節(jié)點(diǎn) Citus 集群所需的步驟。
在所有節(jié)點(diǎn)上執(zhí)行的步驟
1、添加倉庫
# Add Citus repository for package manager
curl https://install.citusdata.com/community/deb.sh | sudo bash
2、安裝 PostgreSQL + Citus 并初始化數(shù)據(jù)庫
# install the server and initialize db
sudo apt-get -y install postgresql-14-citus-10.2
# preload citus extension
sudo pg_conftool 14 main set shared_preload_libraries citus
這會(huì)在 /etc/postgresql/14/main 中安裝集中配置,并在 /var/lib/postgresql/14/main 中創(chuàng)建數(shù)據(jù)庫。
3、配置連接和認(rèn)證
在啟動(dòng)數(shù)據(jù)庫之前,讓我們更改其訪問權(quán)限。默認(rèn)情況下,數(shù)據(jù)庫服務(wù)器僅偵聽 localhost 上的客戶端。作為此步驟的一部分,我們指示它偵聽所有 IP 接口,然后配置客戶端身份驗(yàn)證文件以允許來自本地網(wǎng)絡(luò)的所有傳入連接。
sudo pg_conftool 14 main set listen_addresses '*'
sudo vi /etc/postgresql/14/main/pg_hba.conf
# Allow unrestricted access to nodes in the local network. The following ranges
# correspond to 24, 20, and 16-bit blocks in Private IPv4 address spaces.
host all all 10.0.0.0/8 trust
# Also allow the host unrestricted access to connect to itself
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
您的 DNS 設(shè)置可能不同。此外,這些設(shè)置對(duì)于某些環(huán)境來說過于寬松,請(qǐng)參閱我們關(guān)于提高工作人員安全性的說明。PostgreSQL 手冊(cè)解釋了如何使它們更具限制性。
4、啟動(dòng)數(shù)據(jù)庫服務(wù)器,創(chuàng)建 Citus 擴(kuò)展
# start the db server
sudo service postgresql restart
# and make it start automatically when computer does
sudo update-rc.d postgresql enable
您必須將 Citus 擴(kuò)展添加到要在集群中使用的每個(gè)數(shù)據(jù)庫。以下示例將擴(kuò)展名添加到名為 postgres 的默認(rèn)數(shù)據(jù)庫中。
# add the citus extension
sudo -i -u postgres psql -c "CREATE EXTENSION citus;"
在 coordinator 節(jié)點(diǎn)上執(zhí)行的步驟
下面列出的步驟必須僅在執(zhí)行了前面提到的步驟之后在 coordinator 節(jié)點(diǎn)上執(zhí)行。
1、添加 worker 節(jié)點(diǎn)信息
我們需要通知 coordinator 有關(guān)其 worker 的信息。為了添加這些信息,我們調(diào)用一個(gè) UDF,它將節(jié)點(diǎn)信息添加到 pg_dist_node 目錄表中。對(duì)于我們的示例,我們假設(shè)有兩個(gè) worker(名為 worker-101,worker-102)。將 worker 的 DNS 名稱(或 IP 地址)和服務(wù)器端口添加到表中。
sudo -i -u postgres psql -c "SELECT * from citus_add_node('worker-101', 5432);"
sudo -i -u postgres psql -c "SELECT * from citus_add_node('worker-102', 5432);"
2、驗(yàn)證安裝是否成功
為了驗(yàn)證安裝是否成功,我們檢查 coordinator 節(jié)點(diǎn)是否選擇了所需的工作配置。這個(gè)命令在 psql shell 中運(yùn)行時(shí)應(yīng)該輸出我們添加到上面 pg_dist_node 表中的 worker 節(jié)點(diǎn)。
sudo -i -u postgres psql -c "SELECT * FROM citus_get_active_worker_nodes();"
準(zhǔn)備使用 Citus:
在此步驟中,您已完成安裝過程并準(zhǔn)備好使用 Citus 集群??梢酝ㄟ^ postgres 用戶在 psql 中訪問新的 Citus 數(shù)據(jù)庫:
sudo -i -u postgres psql
Fedora、CentOS 或 Red Hat
本節(jié)介紹在您自己的 Linux 機(jī)器上使用 RPM 包設(shè)置多節(jié)點(diǎn) Citus 集群所需的步驟。
在所有節(jié)點(diǎn)上執(zhí)行的步驟
1、添加倉庫
# Add Citus repository for package manager
curl https://install.citusdata.com/community/rpm.sh | sudo bash
2、安裝 PostgreSQL + Citus 并初始化數(shù)據(jù)庫
# install PostgreSQL with Citus extension
sudo yum install -y citus102_14
# initialize system database (using RHEL 6 vs 7 method as necessary)
sudo service postgresql-14 initdb || sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
# preload citus extension
echo "shared_preload_libraries = 'citus'" | sudo tee -a /var/lib/pgsql/14/data/postgresql.conf
PostgreSQL 在 /usr/pgsql-14/bin 中添加了特定于版本的二進(jìn)制文件,但您通常只需要 psql,它的最新版本已添加到您的路徑中,并且可以使用 service 命令來管理服務(wù)器本身。
3、配置連接和認(rèn)證
在啟動(dòng)數(shù)據(jù)庫之前,讓我們更改其訪問權(quán)限。默認(rèn)情況下,數(shù)據(jù)庫服務(wù)器僅偵聽 localhost 上的客戶端。作為此步驟的一部分,我們指示它偵聽所有 IP 接口,然后配置客戶端身份驗(yàn)證文件以允許來自本地網(wǎng)絡(luò)的所有傳入連接。
sudo vi /var/lib/pgsql/14/data/postgresql.conf
# Uncomment listen_addresses for the changes to take effect
listen_addresses = '*'
sudo vi /var/lib/pgsql/14/data/pg_hba.conf
# Allow unrestricted access to nodes in the local network. The following ranges
# correspond to 24, 20, and 16-bit blocks in Private IPv4 address spaces.
host all all 10.0.0.0/8 trust
# Also allow the host unrestricted access to connect to itself
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
您的 DNS 設(shè)置可能不同。此外,這些設(shè)置對(duì)于某些環(huán)境來說過于寬松,請(qǐng)參閱我們關(guān)于提高 Worker 安全性的說明。PostgreSQL 手冊(cè)解釋了如何使它們更具限制性。
https://docs.citusdata.com/en/v10.2/admin_guide/cluster_management.html#worker-security。
4、啟動(dòng)數(shù)據(jù)庫服務(wù)器,創(chuàng)建 Citus 擴(kuò)展
# start the db server
sudo service postgresql-14 restart
# and make it start automatically when computer does
sudo chkconfig postgresql-14 on
您必須將 Citus 擴(kuò)展添加到要在集群中使用的每個(gè)數(shù)據(jù)庫。以下示例將擴(kuò)展名添加到名為 postgres 的默認(rèn)數(shù)據(jù)庫中。
sudo -i -u postgres psql -c "CREATE EXTENSION citus;"
在 coordinator 節(jié)點(diǎn)上執(zhí)行的步驟
下面列出的步驟必須僅在執(zhí)行了前面提到的步驟之后在 coordinator 節(jié)點(diǎn)上執(zhí)行。
1、添加 worker 節(jié)點(diǎn)信息
我們需要通知 coordinator 有關(guān)其 worker 的信息。為了添加這些信息,我們調(diào)用一個(gè) UDF,它將節(jié)點(diǎn)信息添加到 pg_dist_node 目錄表中。對(duì)于我們的示例,我們假設(shè)有兩個(gè) worker(名為 worker-101,worker-102)。將 worker 的 DNS 名稱(或 IP 地址)和服務(wù)器端口添加到表中。
sudo -i -u postgres psql -c "SELECT * from citus_add_node('worker-101', 5432);"
sudo -i -u postgres psql -c "SELECT * from citus_add_node('worker-102', 5432);"
2、驗(yàn)證安裝是否成功
為了驗(yàn)證安裝是否成功,我們檢查 coordinator 節(jié)點(diǎn)是否選擇了所需的工作配置。這個(gè)命令在 psql shell 中運(yùn)行時(shí)應(yīng)該輸出我們添加到上面 pg_dist_node 表中的 worker 節(jié)點(diǎn)。
sudo -i -u postgres psql -c "SELECT * FROM citus_get_active_worker_nodes();"
準(zhǔn)備使用 Citus:
在此步驟中,您已完成安裝過程并準(zhǔn)備好使用 Citus 集群??梢酝ㄟ^ postgres 用戶在 psql 中訪問新的 Citus 數(shù)據(jù)庫:
sudo -i -u postgres psql