在CentOS系統上安裝PostgreSQL數據庫,可以按照以下步驟進行:
更新系統包
sudo yum update -y
啟用PostgreSQL倉庫
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安裝PostgreSQL
sudo yum install -y postgresql12-server
這里以安裝PostgreSQL 12為例,你可以根據需要選擇其他版本。
初始化數據庫集群
sudo postgresql-setup --initdb
啟動PostgreSQL服務
sudo systemctl start postgresql
設置開機自啟
sudo systemctl enable postgresql
創建數據庫和用戶
sudo -u postgres psql
CREATE DATABASE mydatabase;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
退出psql
\q
如果你需要安裝特定版本的PostgreSQL或者有特殊需求,可以選擇從源碼編譯安裝。
安裝依賴包
sudo yum groupinstall -y "Development Tools"
sudo yum install -y readline-devel zlib-devel
下載PostgreSQL源碼 訪問PostgreSQL官方下載頁面,選擇合適的版本并下載。
wget https://ftp.postgresql.org/pub/source/v13.3/postgresql-13.3.tar.gz
tar -zxvf postgresql-13.3.tar.gz
cd postgresql-13.3
配置編譯選項
./configure --prefix=/usr/local/pgsql --with-includes=/usr/include --with-libraries=/usr/lib64
編譯并安裝
make
sudo make install
初始化數據庫集群
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
啟動PostgreSQL服務
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start
設置環境變量
編輯~/.bash_profile
或~/.bashrc
文件,添加以下內容:
export PATH=/usr/local/pgsql/bin:$PATH
然后執行:
source ~/.bash_profile
創建數據庫和用戶
/usr/local/pgsql/bin/psql -U postgres
CREATE DATABASE mydatabase;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
退出psql
\q
通過以上步驟,你應該能夠在CentOS系統上成功安裝并配置PostgreSQL數據庫。