在CentOS 7 / RHEL 7安裝PostgreSQL 10


CentOS 到了7.x版本, PostgreSQL也來到了10.x版本. 前些天MySQL都直接跨到了8.0版本.

 

本文是一篇在CentOS 7.4上安裝安裝PostgreSQL 10.3 的教程. 本文發布於2018-04-28.

1. 切換到root用戶:
sudo su
2. 把最新的rpm包添加到系統庫:

PostgreSQL會為所有的Linux平台發布rpm包, 而且會比其他的的庫更新的更快.
可以試試在瀏覽器輸入 https://download.postgresql.org/pub 看會發現什么.

### CentOS 7 ###

# rpm -Uvh https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

### RHEL 7 ###

# rpm -Uvh https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat10-10-2.noarch.rpm
3. 用 yum 安裝PostgreSQL:
# yum install -y postgresql10-server postgresql10
4. 安裝完成后, 初次使用前先進行初始化:
# /usr/pgsql-10/bin/postgresql-10-setup initdb

PostgreSQL文件默認放在路徑: /var/lib/pgsql/10/data/

5. 啟動PostgreSQL:
# systemctl start postgresql-10
6. 設置PostgreSQL自啟動:
# systemctl enable postgresql-10
7. 查詢PostgreSQL運行狀態:
# systemctl status postgresql-10

會輸出類似的信息:

● postgresql-10.service - PostgreSQL 10 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-10.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-04-27 18:03:08 CST; 21h ago
     Docs: https://www.postgresql.org/docs/10/static/
 Main PID: 14916 (postmaster)
   CGroup: /system.slice/postgresql-10.service
           ├─14916 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/
           ├─14920 postgres: logger process
           ├─14922 postgres: checkpointer process
           ├─14923 postgres: writer process
           ├─14924 postgres: wal writer process
           ├─14925 postgres: autovacuum launcher process
           ├─14926 postgres: stats collector process
           └─14927 postgres: bgworker: logical replication launcher

Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ systemd[1]: Starting PostgreSQL 10 database server...
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.762 CST [14916] LOG:  listening on IPv4 address "127.0.0.1...t 5432
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.762 CST [14916] LOG:  could not bind IPv6 address "::1": C...ddress
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.762 CST [14916] HINT:  Is another postmaster already runni...retry.
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.764 CST [14916] LOG:  listening on Unix socket "/var/run/p....5432"
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.767 CST [14916] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.777 CST [14916] LOG:  redirecting log output to logging co...rocess
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ postmaster[14916]: 2018-04-27 18:03:08.777 CST [14916] HINT:  Future log output will appear in di..."log".
Apr 27 18:03:08 iZbp144wg7yw6hrfyu76anZ systemd[1]: Started PostgreSQL 10 database server.
Hint: Some lines were ellipsized, use -l to show in full.

8. 再確認看看PostgreSQL是否運行在5432端口:

# netstat -antup | grep 5432

會有類似輸出:

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      14916/postmaster
9. 連接PostgreSQL服務器:

PostgreSQL會自動創建postgres用戶, 創建數據庫之前, 要用postgres用戶或root用戶登錄並重置postgres用戶密碼.

# su -l postgres

 

10. 連接數據庫, psql命令會激活PostgreSQL數據庫終端:
$ psql

 

會輸出下列說明連接進入了PostgreSQL數據庫:

psql (10.3)
Type "help" for help.

postgres=# 
11. 重置postgres用戶密碼:
postgres=# \password

密碼:postgresql


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM