動態

詳情 返回 返回

PostgreSQL 18 源碼編譯安裝體驗 - 動態 詳情

PostgreSQL 18 於前幾個小時剛剛發佈,來個一鍵安裝(Ubuntu 20.0)

image

一鍵安裝腳本,全自動編譯安裝,兩個實例的安裝pg1800和pg1900也只是1分鐘的事,自定義各級目錄,乾淨清晰。
前兩天羣裏竟然還有人推崇apt/yum安裝,説是統一規範,apt/yum安裝出來的目錄結構亂七八的,反規範吧,難道是那個人不會編譯安裝?
image

源碼包地址:https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0.tar.gz
自動安裝腳本

#!/bin/bash

# 1,PostgreSQL源碼包名稱(假設已下載並位於當前目錄的同級目錄中)
POSTGRESQL_SRC="postgresql-18.0.tar.gz"  # 替換XX.X為你的PostgreSQL版本
POSTGRESQL_DIR="postgresql-18.0"         # 解壓後的目錄名,同樣替換XX.X

# 2,安裝目錄
INSTALL_DIR="/usr/local/pgsql18/server"

# 3,端口號
PORT=1800

# 4,初始密碼(通過參數傳入,默認為空)
INIT_PASSWORD="postgres_init_pwd"

if [ $# -gt 0 ]; then
    INIT_PASSWORD="$1"
fi

# 實例目錄
INSTANCE_DATA_DIR="/usr/local/pgsql18/pg${PORT}/data"
INSTANCE_LOG_DIR="/usr/local/pgsql18/pg${PORT}/log"

# 創建postgres用户(如果尚未存在)
if ! id postgres &>/dev/null; then
    echo "Creating postgres user..."
    sudo groupadd postgres
    sudo useradd -m -g postgres postgres -s /bin/bash
    echo "Postgres user created successfully."
else
    echo "Postgres user already exists."
fi

# 檢查目錄是否存在
if [ ! -d "$INSTALL_DIR" ]; then
  echo "dir '$INSTALL_DIR' not existing,creating..."
  mkdir -p "$INSTALL_DIR"
  echo "dir '$INSTALL_DIR' created"
else
  echo "dir '$INSTALL_DIR' existing"
fi

# 檢查實例目錄是否存在
if [ ! -d "$INSTANCE_DATA_DIR" ]; then
  echo "dir '$INSTANCE_DATA_DIR' not existing,creating..."
  mkdir -p "$INSTANCE_DATA_DIR"
  mkdir -p "$INSTANCE_LOG_DIR"
  echo "dir '$INSTANCE_DATA_DIR' created"
else
  echo "dir '$INSTANCE_DATA_DIR' existing"
fi

# 安裝編譯依賴項
echo "############################Installing build dependencies... "
sudo apt update -y > /dev/null 2>&1
sudo apt install -y systemtap-sdt-dev libicu-dev libreadline-dev zlib1g-dev libssl-dev libpam0g-dev libxml2-dev libxslt1-dev libldap2-dev libsystemd-dev tcl-dev libpython3-dev libperl-dev libicu-dev pkg-config  > /dev/null 2>&1
echo "############################Done."

# 解壓源碼包
echo "############################Extracting PostgreSQL source code... "
tar -xzvf "$POSTGRESQL_SRC" > /dev/null 2>&1
echo "############################Done."


# 進入解壓後的目錄
cd "$POSTGRESQL_DIR"

# 配置PostgreSQL(這裏使用默認配置,但可以添加--prefix等選項)
echo "############################Configuring PostgreSQL... "
#make clean
./configure --prefix="$INSTALL_DIR" --with-openssl > /dev/null 2>&1
echo "############################Done."

# 編譯PostgreSQL
echo -n "############################Compiling PostgreSQL (this may take a while)... "
# pg 17之前,用make world -j$(nproc)  > /dev/null 2>&1
make world-bin -j$(nproc)  > /dev/null 2>&1
echo "############################Done."

# 安裝PostgreSQL
echo "############################Installing PostgreSQL... "
# pg 17之前,用make install-world > /dev/null 2>&1
sudo make install-world-bin > /dev/null 2>&1
echo "############################Done."

# 初始化數據庫(使用postgres用户)
echo "############################Initializing PostgreSQL database..."
chown -R postgres:postgres $INSTALL_DIR
chmod 700 -R $INSTALL_DIR

chown -R postgres:postgres $INSTANCE_DATA_DIR
chmod 700 -R $INSTANCE_DATA_DIR

chown -R postgres:postgres $INSTANCE_LOG_DIR
chmod 700 -R $INSTANCE_LOG_DIR

sudo -u postgres $INSTALL_DIR/bin/initdb -D $INSTANCE_DATA_DIR

# 1. 創建systemd服務(使用指定模板)
echo "############################Creating systemd service..."
cat <<EOF | sudo tee /etc/systemd/system/postgresql${PORT}.service
[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking
User=postgres
Group=postgres
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0
#PGSTARTTIMEOUT should be less than TimeoutSec value.
Environment=PGSTARTTIMEOUT=300
Environment=PGDATA=${INSTANCE_DATA_DIR}
ExecStart=${INSTALL_DIR}/bin/pg_ctl start -D \${PGDATA} -s -w -t \${PGSTARTTIMEOUT}
ExecStop=${INSTALL_DIR}/bin/pg_ctl stop -D \${PGDATA} -s -m fast
ExecReload=${INSTALL_DIR}/bin/pg_ctl reload -D \${PGDATA} -s
TimeoutSec=300
KillMode=mixed

[Install]
WantedBy=multi-user.target
EOF

# 2. 修改pg_hba.conf
echo "############################Modifying pg_hba.conf..."
sudo -u postgres sed -i "s/^host.*all.*all.*127.0.0.1\/32.*trust/host all all 0.0.0.0\/0 md5/" ${INSTANCE_DATA_DIR}/pg_hba.conf
sudo -u postgres sed -i "s/^host.*all.*all.*::1\/128.*trust/host all all ::0\/0 md5/" ${INSTANCE_DATA_DIR}/pg_hba.conf

# 3. 修改postgresql.conf
echo "############################Modifying postgresql.conf..."
sudo -u postgres sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" ${INSTANCE_DATA_DIR}/postgresql.conf
sudo -u postgres sed -i "s/^#port = 5432/port = ${PORT}/" ${INSTANCE_DATA_DIR}/postgresql.conf

# 打開日誌
sed -i "s/^#logging_collector.*/logging_collector = on/" ${INSTANCE_DATA_DIR}/postgresql.conf
sed -i "s/^#log_destination.*/log_destination = 'stderr, csvlog'/" ${INSTANCE_DATA_DIR}/postgresql.conf
sed -i "s|^#log_directory.*|log_directory = 'log'|" ${INSTANCE_DATA_DIR}/postgresql.conf
sed -i "s/^#log_filename.*/log_filename = 'postgresql-%Y-%m-%d.log'/" ${INSTANCE_DATA_DIR}/postgresql.conf
sed -i "s/^#log_rotation_age.*/log_rotation_age = 1d/" ${INSTANCE_DATA_DIR}/postgresql.conf


# 啓用並啓動服務
echo "############################Starting PostgreSQL service..."
sudo systemctl daemon-reload
sudo systemctl enable postgresql${PORT}
sudo systemctl start postgresql${PORT}

# 4. 修改postgres用户密碼(如果提供了初始密碼)
if [ -n "$INIT_PASSWORD" ]; then
    echo "Changing postgres user password..."
    sleep 5  # 等待PostgreSQL完全啓動
    sudo -u postgres ${INSTALL_DIR}/bin/psql -p ${PORT} -c "ALTER USER postgres WITH PASSWORD '${INIT_PASSWORD}';"
    echo "Postgres user password changed successfully."
else
    echo "No initial password provided, keeping default password."
fi


# 5. 修改環境變量
echo "############################Checking and setting up PostgreSQL environment variables in /etc/profile..."
ENV_CONTENT="# PostgreSQL Environment (added by install script)
export PGHOME=${INSTALL_DIR%} 
export PATH=\$PATH:\$PGHOME/bin"
# 檢查是否已存在相關配置
if ! grep -q "PGBASE=${INSTALL_DIR%}" /etc/profile; then
    echo "Adding PostgreSQL environment variables to /etc/profile..."
    echo "$ENV_CONTENT" | sudo tee -a /etc/profile > /dev/null
    source /etc/profile    
    echo "Environment variables added successfully."
else
    echo "PostgreSQL environment variables already exist in /etc/profile, skipping..."
fi


# 打印完成消息
echo "############################PostgreSQL installation and database initialization completed."
echo "############################PostgreSQL is now running on port ${PORT}"
if [ -n "$INIT_PASSWORD" ]; then
    echo "You can connect using: psql -h 127.0.0.1 -p ${PORT} -U postgres -W"
    echo "Password: ${INIT_PASSWORD}"
else
    echo "You can connect using: psql -h 127.0.0.1 -p ${PORT} -U postgres"
fi

# 回到腳本的起始目錄(如果需要的話)
cd -

 

Add a new 評論

Some HTML is okay.