1. 程式人生 > 其它 >Cenots7 離線安裝部署PostgreSQL

Cenots7 離線安裝部署PostgreSQL

PostgreSQL原始碼包下載並複製

PostgreSQL原始碼包下載

訪問PostgreSQL官網:https://www.postgresql.org/ftp/source/

選擇所需版本進行下載,本次下載安裝版本為 v14.5

複製原始碼包至伺服器

使用SSH終端工具,遠端連線伺服器,並使用終端工具提供的上傳工具,把postgresql-14.5.tar.gz 上傳至伺服器 /home/postgres14.5 資料夾下

建目錄資料夾的命令

[root@localhost local]# mkdir -p /home/postgres14.5

基於PostgreSQL原始碼安裝

切換到原始碼目錄

[root@localhost local]# cd /home/postgres14.5

解壓gz

[root@localhost postgres14.5]# gunzip postgresql-14.5.tar.gz

解壓tar

[root@localhost postgres14.5]# tar -xf postgresql-14.5.tar

檢查環境 指定安裝路徑

檢查環境,指定安裝目錄和服務埠

[root@localhost postgresql14.5]# cd postgresql-14.5
# 安裝位置由 --prefix=/usr/local/pgsql-14.5中指定,埠號由 --with-pgport=5435 指定
[root@localhost postgresql14.5]# ./configure --prefix=/usr/local/pgsql-14.5 --with-pgport=5435

注意:使用configure指令碼檢查,無錯誤或警告提示方可進行下一步編譯操作,若有錯誤或警告提示需根據提示進行相關操作。

安裝時出現:configure:error:readline library not found

要安裝 readline , readline-dev 開發包,要麼使用 --without-readline 選項關閉 readline 功能。

# readline 也就是命令列編輯,關閉的話,你直接用psql 就不能編輯命令列,如果輸錯指令,不能回滾命令歷史記錄,只能手工重新輸入。
yum install readline;
yum install readline-dev;

編譯

[root@localhost postgresql-14.5]# make

安裝

[root@localhost postgresql-14.5]# make install

postgresql的配置

建立使用者和組

建立組

[root@localhost ~]# groupadd postgres

建立使用者並加入組

[root@localhost pgsql-14.5]#useradd -g postgres postgres

建立資料庫庫檔案儲存目錄、給postgres賦予許可權

建立資料庫庫檔案儲存目錄data

[root@localhost pgsql-14.5]# mkdir -p /run/media/postgres/data

data目錄授權給postgres.postgres

[root@localhost pgsql-14.5]# chown postgres.postgres /run/media/postgres/data

初始化資料庫目錄

切換使用者

[root@localhost pgsql-14.5]# su postgres

初始化資料 -D指定初始化建立的資料庫的檔案路徑

[postgres@wc-ckg-99-2 root]$ /usr/local/pgsql-14.5/bin/initdb -D /run/media/postgres/data

紅框中標註為postgres14.5的啟動方式

/usr/local/pgsql-14.5/bin/pg_ctl -D /run/media/postgres/data -l logfile start

啟動停止postgres14.5

啟動

切換使用者 PG是禁止使用超級管理員來執行該命令的

[root@localhost pgsql-14.5]# su postgres

啟動資料庫

[postgres@wc-ckg-99-2 root]$ /usr/local/pgsql-14.5/bin/pg_ctl -D /run/media/postgres/data -l logfile start
# 停止
[postgres@wc-ckg-99-2 root]$ /usr/local/pgsql-14.5/bin/pg_ctl -D /run/media/postgres/data -l logfile stop

執行啟動命令許可權不夠

編輯sudoers配置檔案 ,按下圖紅框所示,給postgres使用者新增提升許可權的配置

[root@localhost bin]# vi /etc/sudoers

修改管理員密碼

psql命令目錄:/usr/local/pgsql-14.5/bin

# 在/ usr / bin中建立一個postgre14.5版本對應的psql連結
[root@localhost bin]# ln -s /usr/local/pgsql-14.5/bin/psql /usr/bin/psql145
# 切換使用者
[root@localhost bin]# su - postgres
# 執行命令
[postgres@wc-ckg-99-2 root]$ psql145

could not change directory to "/root": Permission denied
psql145 (14.5)
Type "help" for help.

# postgres=# 修改管理員密碼,\q退出
postgres=# alter role postgres with password '123';
postgres=# \q;

開啟遠端訪問

切換到資料庫目錄

[root@localhost bin]# cd /run/media/postgres/data

修改postgresql.conf 配置檔案,開啟遠端訪問

listen_addresses = 'localhost',修改成 listen_addresses = '*'

[root@localhost data]# vi postgresql.conf

配置認證方式

修改/run/media/postgres/data/pg_hba.conf 新增遠端訪問的認證方式,未尾新增 host all all 0.0.0.0/0 md5

[root@localhost data]# vi pg_hba.conf

測試連線

基本語法

sql建立資料庫、表增刪改查都大同小異。

不同命令

執行sql檔案

[root@localhost bin]# ./psql -U postgres -h localhost -p 5432 -d 資料庫名 -f xxx.sql

建立資料庫

# 建立以 utf-8 字元的資料庫,並且以 template0 為模版建立
create database 資料庫名 with  owner = postgres template = template0 encoding = 'utf8';
# 給指定使用者授指定資料庫所有許可權
grant all privileges on database 資料庫名 to postgres;

執行登陸操作後提示 FATAL: role 'root' is not permitted to log in.,執行命令 alter user "root" login;
執行命令後提示 Postgresql : syntax error at or near "-",將名字新增 "",eg: alter user "dell-sys" with password 'Pass@133';


原文章地址:
https://www.cnblogs.com/starxing/p/16816122.html