一、下載安裝:
1、下載:
官網下載地址:https://www.postgresql.org/download/linux/redhat/
也可以用這個:https://www.enterprisedb.com/download-postgresql-binaries
從上述地址可以看到,其實官方從11開始已經沒有二進制版本提供了,都是使用對應平台的發行版yum安裝了。
去官網看other linux,已經沒有最新的二進制包了。
https://www.postgresql.org/download/linux/#generic
2、安裝:
我們使用官方提供的yum方式安裝
https://www.postgresql.org/download/linux/redhat/
選擇對應的平台與版本
# Install the repository RPM: sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # Install PostgreSQL: sudo yum install -y postgresql13-server # Optionally initialize the database and enable automatic start: sudo /usr/pgsql-13/bin/postgresql-13-setup initdb sudo systemctl enable postgresql-13 sudo systemctl start postgresql-13
二、初始化配置:
默認安裝目錄:
bin目錄:/usr/pgsql-13/bin
data目錄:/var/lib/pgsql/13/data
1、修改data目錄
# 暫停服務 systemctl stop postgresql-13 # 移動data目錄: mv /var/lib/pgsql /data/apps/pgsql # 修改配置,第46行 vim /data/apps/pgsql/13/data/postgresql.conf ... data_directory = '/data/apps/pgsql/13/data' ... # 修改postgres用戶家目錄,否則使用su - postgres的時候會報錯:su: warning: cannot change directory to /var/lib/pgsql: No such file or directory vim /etc/passwd ... postgres:x:26:26:PostgreSQL Server:/data/apps/pgsql:/bin/bash # 修改啟動項里的data目錄路徑PGDATA vim /usr/lib/systemd/system/postgresql-13.service ... Environment=PGDATA=/data/apps/pgsql/13/data/ ... # 啟動服務 systemctl daemon-reload systemctl start postgresql-13.service
2、登錄修改超級用戶密碼:
需要從root切換到postgres用戶,修改密碼:
# 切換用戶到postgres [root@dev-predictionio-206 bin]# su - postgres Last login: Mon Sep 6 10:22:28 CST 2021 on pts/0 # 直接用postgres超級用戶登錄,默認不需要密碼,psql直接回車就以postgres用戶進入了postgres數據庫 -bash-4.2$ psql -U postgres psql (13.4) Type "help" for help. # 修改超級用戶密碼為:postgres postgres=# alter role postgres with password 'postgres'; ALTER ROLE # 退出 postgres-# \q
在postgres用戶下重載配置
修改用戶
# 修改用戶認證模式trust為md5 bash-4.2$ vim /data/apps/pgsql/13/data/pg_hba.conf # IPv4 local connections: host all all all md5 host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::1/128 md5 # 重載配置 -bash-4.2$ export PGPORT=5432 -bash-4.2$ export PGDATE=postgres -bash-4.2$ export PGDATA=/data/apps/pgsql/13/data -bash-4.2$ export PGUSER=postgres -bash-4.2$ vim /etc/profile -bash-4.2$ source /etc/profile -bash-4.2$ pg_ctl reload server signaled -bash-4.2$ exit logout
3、創建新用戶:
如上所述,先切換到Linux用戶postgres,並執行psql:
[root@dev-predictionio-206 data]# su - postgres Last login: Mon Sep 6 13:57:07 CST 2021 on pts/4 -bash-4.2$ psql psql (13.4) Type "help" for help. postgres=# # 現在位於數據庫提示符下。 # 創建數據庫新用戶,如 dbuser: postgres=# CREATE USER wangshuangxi WITH PASSWORD '123456'; CREATE ROLE postgres=# CREATE USER dbuser WITH PASSWORD '*****'; 注意: 語句要以分號結尾。 密碼要用單引號括起來。 3、創建用戶數據庫,如exampledb: postgres=# CREATE DATABASE exampledb OWNER dbuser; 4、將exampledb數據庫的所有權限都賦予dbuser: postgres=# GRANT ALL PRIVILEGES ON DATABASE exampledb TO dbuser; 5、使用命令 \q 退出psql: postgres=# \q 6、創建Linux普通用戶,與剛才新建的數據庫用戶同名,如 dbuser: $ sudo adduser dbuser $ sudo passwd dbuser 7、以dbuser的身份連接數據庫exampledb: $ su - dbuser Password: Last login: Wed Mar 1 11:52:07 CST 2017 on pts/ [dbuser@master ~]$ psql -d exampledb
4、開啟遠程訪問:
# 修改配置文件,添加監聽地址,改為* vim postgresql.conf listen_addresses = '*' # 修改pg_hba.conf,添加遠程主機地址,放在第一行:允許任意用戶從任意機器上以密碼方式訪問數據庫,把下行添加為第一條規則: host all all 0.0.0.0/0 md5 # 重啟服務: sudo systemctl restart postgresql-13.service
5、通過navicat連接訪問:
連接成功如下:
三、安裝pgadmin圖形管理界面
參考:https://www.cnblogs.com/whitebai/p/13094540.html
查看官網安裝方式:https://www.pgadmin.org/download/pgadmin-4-rpm/
我們選擇rpm包的安裝方式:(也可以選擇python或者其他方式)
# 安裝基礎repo包 sudo rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-2-1.noarch.rpm # To install pgAdmin, run one of the following commands: # Install for both desktop and web modes. # 同時安裝桌面版與web版 sudo yum install pgadmin4 # Install for desktop mode only. sudo yum install pgadmin4-desktop # Install for web mode only. # 只安裝web版,(我們只需要安裝這個就可以了) sudo yum install pgadmin4-web
安裝過程有點長,可能需要輸入y確認。
安裝完成后:啟動web初始化配置:
Finally, if you have installed pgadmin4 or pgadmin4-web, run the web setup script to configure the system to run in web mode:
# sudo /usr/pgadmin4/bin/setup-web.sh Setting up pgAdmin 4 in web mode on a Redhat based platform... Creating configuration database... NOTE: Configuring authentication for SERVER mode. Enter the email address and password to use for the initial pgAdmin user account: Email address: ops@lgitt.com Password: Retype password: pgAdmin 4 - Application Initialisation ====================================== Creating storage and log directories... Configuring SELinux... setsebool: SELinux is disabled. setsebool: SELinux is disabled. The Apache web server is not running. We can enable and start the web server for you to finish pgAdmin 4 installation. Continue (y/n)? y Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. Apache successfully enabled. Apache successfully started. You can now start using pgAdmin 4 in web mode at http://127.0.0.1/pgadmin4
輸入地址:http://192.168.90.206/pgadmin4/
如下:
登錄后如圖所示:
四、安裝插件mysql-fdw (可選)
網上很多編譯安裝的方法,官網也是編譯安裝的,編譯安裝依賴包太多,因為只是插件,這里直接采用yum安裝:
yum安裝mysql-fdw
# 首先查找一下fdw yum search postgresql|grep fdw # 找到了對應包名,安裝: yum install mysql_fdw_13
源碼編譯安裝參考:https://www.cnblogs.com/funnyzpc/p/14223167.html
准備libmysqlclient
注意:若mysql與postgresql在同一台linux機上,則無需安裝mysql工具,請略過本段
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.22-linux-glibc2.17-x86_64-minimal.tar.xz tar -xf mysql-8.0.22-linux-glibc2.17-x86_64-minimal.tar.xz mv mysql-8.0.22-linux-glibc2.17-x86_64-minimal /data/apps/mysql-client chown -R mysql:mysql /data/apps/mysql-client
配置環境變量
- 配置文件
vi /etc/profile
- 添加mysql環境變量
# mysql-client export MYSQL_HOME=/data/apps/mysql-client export PATH=$PATH:$MYSQL_HOME/bin export LD_LIBRARY_PATH=$PG_HOME/lib:$MYSQL_HOME/lib:$LD_LIBRARY_PATH
確保pgsql的環境變量存在:
# pgsql export PG_HOME=/usr/pgsql-13/ export PATH=$PATH:$PG_HOME/bin
刷新配置
source /etc/profile
下載並編譯mysql_fdw
下載地址:
https://github.com/EnterpriseDB/mysql_fdw/releases
tar xf mysql_fdw-REL-2_6_0.tar.gz cd mysql_fdw-REL-2_6_0 make USE_PGXS=1 make USE_PGXS=1 install
編譯報錯:
# make USE_PGXS=1 Makefile:39: /usr/pgsql-13/lib/pgxs/src/makefiles/pgxs.mk: No such file or directory Makefile:44: *** PostgreSQL 9.6, 10, 11, 12, or 13 is required to compile this extension. Stop.
解決辦法:
yum install postgresql13-devel -y
如果遇到依賴報錯:
--> Finished Dependency Resolution Error: Package: postgresql13-devel-13.4-1PGDG.rhel7.x86_64 (pgdg13) Requires: llvm-toolset-7-clang >= 4.0.1 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest
解決辦法:
yum install centos-release-scl-rh -y yum install postgresql13-devel -y
編譯完,重啟pgsql
systemctl restart postgresql-13.service