切換到root用戶
su root
在CentOS 7上安裝PostgreSQL 10
PostgreSQL爲所有Linux平臺發佈rpm包,而且它們的軟件包比OS資源庫中的軟件包更新. 所以,你需要通過安裝repo rpm來在你的機器上添加倉庫。
rpm -Uvh https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
yum list postgresql10*
使用yum命令安裝PostgreSQL 10。
yum install -y postgresql10-server postgresql10
初始化PostgreSQL服務器
安裝PostgreSQL之後,您需要在首次使用之前對其進行初始化。
/ usr / pgsql-10 / bin / postgresql-10-setup initdb
PostgreSQL數據通常位於/ var / lib / pgsql / 10 / data /目錄。
啓動PostgreSQL服務器
要啓動PostgreSQL服務,請運行:
systemctl start postgresql-10
要在系統啓動時啓用PostgreSQL,請運行:
systemctl enable postgresql-10
要檢查PostgreSQL服務的狀態,請運行:
systemctl status postgresql-10
Output:
● postgresql-10.service - PostgreSQL 10 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-10.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2017-10-23 17:54:14 UTC; 6min ago
Main PID: 1557 (postmaster)
CGroup: /system.slice/postgresql-10.service
├─1557 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/
├─1560 postgres: logger process
├─1562 postgres: checkpointer process
├─1563 postgres: writer process
├─1564 postgres: wal writer process
├─1565 postgres: autovacuum launcher process
├─1566 postgres: stats collector process
└─1567 postgres: bgworker: logical replication launcher
Oct 23 17:54:14 post systemd[1]: Starting PostgreSQL 10 database server...
Oct 23 17:54:14 post postmaster[1557]: 2017-10-23 17:54:14.578 UTC [1557] LOG: listening on IPv6 address "::1", port 5432
Oct 23 17:54:14 post postmaster[1557]: 2017-10-23 17:54:14.578 UTC [1557] LOG: listening on IPv4 address "127.0.0.1", port 5432
Oct 23 17:54:14 post postmaster[1557]: 2017-10-23 17:54:14.584 UTC [1557] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Oct 23 17:54:14 post postmaster[1557]: 2017-10-23 17:54:14.587 UTC [1557] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
Oct 23 17:54:14 post postmaster[1557]: 2017-10-23 17:54:14.597 UTC [1557] LOG: redirecting log output to logging collector process
Oct 23 17:54:14 post postmaster[1557]: 2017-10-23 17:54:14.597 UTC [1557] HINT: Future log output will appear in directory "log".
Oct 23 17:54:14 post systemd[1]: Started PostgreSQL 10 database server.
使用netstat命令確認PostgreSQL在端口5432上偵聽。
netstat -antup | grep 5432
輸出:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 21051 / postmaster
tcp6 0 0 ::: 5432 ::: * LISTEN 21051 / postmaster
訪問PostgreSQL服務器 要創建數據庫,請以postgres(Linux用戶)身份登錄。從root用戶登錄或重置postgres用戶的登錄密碼
#su -l postgres
訪問數據庫使用psql命令,一個用於PostgreSQL數據庫的交互式前端終端。
$ psql
輸出:
-bash-4.2$ psql
psql (10.0)
Type "help" for help.
postgres=#
爲postgres(數據庫管理員)用戶設置密碼。 就這樣。您已成功在CentOS 7 / RHEL 7上安裝PostgreSQL 10。
psql 訪問遠程數據庫
切換到root
psql -d testDB -h 10.1.2.11 -p 5432 -U postgres
如果只是安裝postgresql客戶端,用以訪問其他數據
查看安裝包
yum list postgresql10*
可以看到:
Available Packages
postgresql10.x86_64 10.3-1PGDG.rhel7 pgdg10
postgresql10-contrib.x86_64 10.3-1PGDG.rhel7 pgdg10
postgresql10-debuginfo.x86_64 10.3-1PGDG.rhel7 pgdg10
postgresql10-devel.x86_64 10.3-1PGDG.rhel7 pgdg10
postgresql10-docs.x86_64 10.3-1PGDG.rhel7 pgdg10
postgresql10-libs.x86_64 10.3-1PGDG.rhel7 pgdg10
執行安裝postgresql10.x86_64 (僅客戶端)
yum install -y postgresql10.x86_64
安裝完畢,執行遠程訪問某個數據
psql -d testDB -h 10.1.1.1 -p 5432 -U postgres