官網安裝方法:https://www.postgresql.org/download/linux/redhat/
卸載的話使用 yum remove 相應的安裝
Install the repository RPM:
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-1.noarch.rpm
install the server packages:
yum install postgresql10-server
此時在/usr/下多了pgsql-10目錄
關於安裝的幾個軟件的介紹:
postgresql-client libraries and client binaries
postgresql-server core database server
postgresql-contrib additional supplied modules
postgresql-devel libraries and headers for C language development
pgadmin3 - pgAdmin III graphical administration utility
關於server和client的介紹參見:
https://www.postgresql.org/docs/10/static/tutorial-start.html
安裝后:
設置環境變量:
略
初始化數據庫:
/usr/pgsql-10/bin/postgresql-10-setup initdb
可選設置自動啟動:
systemctl enable postgresql-10
systemctl start postgresql-10
啟動服務:
service postgresql-10 initdb
chkconfig postgresql-10 on
直接執行createdb后會有很多問題,具體看https://www.postgresql.org/docs/10/static/tutorial-start.html
PostgreSQL 安裝完成后,會建立一下‘postgres’用戶,用於執行PostgreSQL,數據庫中也會建立一個’postgres’用戶,默認密碼為自動生成,需要在系統中改一下。
修改postgres用戶密碼:
先切換到root
然后passwd postgress
接着輸入兩遍新密碼
修改postgres數據庫管理員密碼:
su - postgres 切換用戶,執行后提示符會變為 ‘-bash-4.2$’,切換為UNIX風格的bash
psql -U postgres 登錄數據庫,執行后提示符變為 ‘postgres=#’
ALTER USER postgres WITH PASSWORD ‘abc123’ 設置postgres用戶密碼
\q 退出數據庫
切換到root配置一下遠程連接。
開啟遠程訪問
vi /var/lib/pgsql/10/data/postgresql.conf
修改#listen_addresses = ‘localhost’ 為 listen_addresses=’*’
當然,此處‘*’也可以改為任何你想開放的服務器IP
信任遠程連接
vi /var/lib/pgsql/10/data/pg_hba.conf
修改如下內容,信任指定服務器連接
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.1.2/32(需要連接的服務器IP) trust
遠程連接配置完成,由於系統原因,還需要在防火牆中打開相應的端口。
打開防火牆
CentOS 防火牆中內置了PostgreSQL服務,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,我們只需以服務方式將PostgreSQL服務開放即可。
firewall-cmd –add-service=postgresql –permanent 開放postgresql服務
firewall-cmd –reload 重載防火牆
最后一步,不能忘記的,是重啟數據庫服務,使配置生效。
重啟PostgreSQL數據服務
systemctl restart postgresql-10.service
錯誤:
Job for postgresql-10.service failed because the control process exited with error code. See "systemctl status postgresql-10.service" and "journalctl -xe" for details
- 1
原因:
數據庫安裝完后沒有進行初始化工作
解決:
>/usr/pgsql-10/bin/postgresql-10-setup initdb
- 1
至此,PostgreSQL 10 在CentOS 7上完成基本安裝和配置。
下面是postgersSQL的使用,在使用postgres的時候要在postgres用戶下使用。
createdb mydb
執行這句的時候回出現很多問題,具體見:doc
下面是常見的一種錯誤:
createdb: could not connect to database postgres: FATAL: role “joe” does not exist
這個是因為沒有以postgres用戶執行的原因。
執行SQL語句:
psql mydb
之后就可以像普通關系型數據庫一樣執行SQL語句了,具體參看doc