CentOS 7 安裝、配置、使用 PostgreSQL 10 安裝及基礎配置
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
安裝後:
設定環境變數:
略
初始化資料庫:
/usr/pgsql-10/bin/postgresql-10-setup initdb
可選設定自動啟動:
systemctl enable postgresql-10
systemctl start postgresql-10
啟動服務:
service postgresql-10 initdb
chkconfig postgresql-10 on
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
關於postgresSQL的圖形介面工具,官方的是pgAdmin官網,可進行安裝使用。
關於工具的連線錯誤請看:pgAdmin doc
連線錯誤一般是由於/var/lib/pgsql/10/data/pg_hba.conf的配置引起的。