天天看點

PostgreSQL安裝安裝PostgreSQL軟體初始化資料庫

環境規劃:

  • 作業系統:CentOS 7.9 64bit
  • 安裝使用者:postgres
  • 軟體安裝路徑:/usr/local/pgsql
  • 資料庫資料目錄:/pgdata

安裝PostgreSQL軟體

1、前置檢查:

make --version       # 版本需要大于等于3.8

rpm -aq |grep readline      # 應該已經提前安裝了readline-devel、readline。

2、以root使用者,建立使用者和屬主:

groupadd postgres
useradd -g postgres postgres
passwd postgres 
#設定使用者新密碼
           

3、以root使用者,建立軟體安裝目錄和資料庫資料目錄,并授權:

mkdir -p /usr/local/pgsql
chown -R postgres:postgres /usr/local/pgsql

mkdir -p /pgdata
chown -R postgres:postgres /pgdata
           

3、選擇需要下載下傳的源代碼

PostgreSQL安裝安裝PostgreSQL軟體初始化資料庫

 4、以postgres使用者,下載下傳,并源代碼方式安裝(軟體安裝目錄為預設的/usr/local/pgsql):

wget https://ftp.postgresql.org/pub/source/v13.4/postgresql-13.4.tar.gz
tar -xvf postgresql-13.4.tar.gz;  cd postgresql-13.4;

./configure        #(--prefix=預設為/usr/local/pgsql)
make
make install
           

其中configure的選項可以參閱:https://www.postgresql.org/docs/current/install-procedure.html#CONFIGURE-OPTIONS

 4、配置環境變量。以postgres使用者,編輯~/.bash_profile,增加以下兩行配置環境變量:

export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH
           

除postgres使用者外,該伺服器上其他需要使用postgreSQL軟體(如用戶端)的使用者,也應該配置以上環境變量。

初始化資料庫

1、以postgres使用者,指定資料庫資料目錄,初始化資料庫

initdb -D /pgdata
           

初始化後,該目錄内容如下:

PostgreSQL安裝安裝PostgreSQL軟體初始化資料庫

 2、由于postgresql.conf檔案是PG服務端的主配置檔案。修改自動生成的/pgdata/postgresql.conf檔案如下,監聽本地所有網卡

PostgreSQL安裝安裝PostgreSQL軟體初始化資料庫

 3、由于pg_hba.conf檔案是基于主機的用戶端認證配置檔案。修改自動生成的/pgdata/pg_hba.conf檔案,對IPv4下面的ADDRESS配置修改如下,允許通過網絡通路。

PostgreSQL安裝安裝PostgreSQL軟體初始化資料庫

4、以postgres使用者,啟動資料庫:

pg_ctl start -D /pgdata

#若停止,則:
pg_ctl stop -D /pgdata [-m SHUTDOWN-MODE]
#  SHUTDOWN-MODE: 有smart、fast、immediate三種選項(預設是fast)。
           

5、以postgres使用者,執行以下psql指令登入資料庫(這裡是根據pg_hba.conf的配置以local本地程序通信方式進行認證,是以無需密碼)。登入成功後檢視包含的資料庫(邏輯資料庫)應該有三個:

PostgreSQL安裝安裝PostgreSQL軟體初始化資料庫

 6、也可以在其他機器上,通過用戶端遠端登入:

PostgreSQL安裝安裝PostgreSQL軟體初始化資料庫

繼續閱讀