I. Cài đặt và cấu hình Postgres

Các hướng dẫn triển khai này được sử dụng để tham khảo khi xây dựng công cụ cardano-db-sync, với mục tiêu là dễ dàng thiết lập và cung cấp một số cơ sở điều chỉnh cho những người mới sử dụng cơ sở dữ liệu Postgres.

1 Cài đặt Postgres.

Thực hiện các lệnh dưới đây để thiết lập máy chủ Postgres.

# Determine OS platform
OS_ID=$( (grep -i ^ID_LIKE= /etc/os-release || grep -i ^ID= /etc/os-release) | cut -d= -f 2)
DISTRO=$(grep -i ^NAME= /etc/os-release | cut -d= -f 2)

if [ -z "${OS_ID##*debian*}" ]; then
  #Debian/Ubuntu
  wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  RELEASE=$(lsb_release -cs)
  echo "deb [arch=amd64] http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}"-pgdg main | sudo tee  /etc/apt/sources.list.d/pgdg.list
  sudo apt-get update
  sudo apt-get -y install postgresql-16 postgresql-server-dev-16 postgresql-contrib libghc-hdbc-postgresql-dev
  sudo systemctl restart postgresql
  sudo systemctl enable postgresql
else
  echo "We have no automated procedures for this ${DISTRO} system"
fi

Tạo người dùng trong Postgres
Đăng nhập vào phiên bản Postgres với quyền superuser:

echo $(whoami)
# <user>
sudo su postgres
psql

Lưu ý giá trị trả về từ lệnh echo $(whoami). Thay thế tất cả các trường hợp của <username> trong tài liệu dưới đây. Thực hiện các lệnh dưới đây trong giao diện psql. Thay thế <username><PasswordYouWant> bằng tên người dùng hệ điều hành của bạn (kết quả từ lệnh echo $(whoami) đã thực thi ở trên) và mật khẩu mà bạn muốn sử dụng để xác thực với Postgres:

CREATE ROLE <user> SUPERUSER LOGIN;
ALTER USER <user> PASSWORD 'PasswordYouWant';
\q

exit tại dòng lệnh để thoát khỏi Postgres và quay trở lại người dùng của bạn.
Xác minh đăng nhập vào phiên bản Postgres

export PGPASSFILE=$CNODE_HOME/priv/.pgpass
echo "/var/run/postgresql:5432:cexplorer:*:*" > $PGPASSFILE
chmod 0600 $PGPASSFILE
psql postgres
# psql (15.0)
# Type "help" for help.
# 
# postgres=#

2. Cấu hình Postgres

Trước khi bạn bắt đầu điền dữ liệu vào cơ sở dữ liệu của mình bằng dữ liệu từ dbsync, dưới đây có thể là cấu hình cơ bản cho phiên bản Postgres của bạn bằng cách chỉnh sửa tệp /etc/postgresql/16/main/postgresql.conf.

ParameterValueComment
data_directory‘/opt/cardano/cnode/guild-db/pgdb/15’Move postgres data directory to ZFS mount at /opt/cardano/cnode, ensure it’s writable by postgres user
effective_cache_size8GBBe conservative as Node and DBSync by themselves will need ~32-40GB of RAM if ledger-state is enabled
effective_io_concurrency4Can go higher if you have substantially higher IOPs/IO throughputs
lc_time‘en_US.UTF-8’Just to use standard server-side time formatting between instances, can adapt to your preferences
log_timezone‘UTC’For consistency, to avoid timezone confusions
maintenance_work_mem512MBHelps with vacuum/index/foreign key maintainance (with 4 workers, it’s set to max 2GB)
max_connections200Allow maximum of 200 connections, the koios connections are still controlled via postgrest db-pool
max_parallel_maintenance_workers4Max workers postgres will use for maintainance
max_parallel_workers4Max workers postgres will use across the system
max_parallel_workers_per_gather2Parallel threads per query, do not increase to higher values as it will multiply memory usage
max_wal_size4GBUsed for WAL automatic checkpoints (disabled later)
max_worker_processes4Maximum number of background processes system can support
min_wal_size1GBUsed for WAL automatic checkpoints (disabled later)
random_page_cost1.1Use higher value if IOPs has trouble catching up (you can use 4 instead of 1.1)
shared_buffers4GBConservative limit to allow for node/dbsync/zfs memory usage
timezone‘UTC’For consistency, to avoid timezone confusions
wal_buffers16MBWAL consumption in shared buffer (disabled later)
work_mem16MBBase memory size before writing to temporary disk files

Ngoài những điều trên, do bản chất sử dụng của dbsync (đồng bộ từ node và khởi động lại việc duyệt ngược trở lại snapshot trạng thái sổ cái đã lưu), chúng ta có thể tận dụng việc đồng bộ dữ liệu trên blockchain – vì chúng ta không bị ảnh hưởng bởi việc mất thông tin tạm thời khi khởi động lại phiên bản. Do đó, chúng ta có thể giảm nhẹ một số thiết lập liên quan đến việc đồng bộ dữ liệu , vì những thiết lập này ảnh hưởng đến IOPs và mức tải CPU. Chúng tôi khuyến nghị thiết lập 3 tùy chọn dưới đây trong tệp /etc/postgresql/16/main/postgresql.conf của bạn:

ParameterValue
wal_levelminimal
max_wal_senders0
synchronous_commitoff

Sau khi bạn đã thực hiện xong các thay đổi, hãy đảm bảo khởi động lại dịch vụ Postgres bằng cách sử dụng lệnh

sudo systemctl restart postgresql.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Nội dung