CentOS 7 安裝 PostgreSQL 教程

CentOS的源中自帶有PostgreSQL,能夠經過 yum list | grep postgresql 查看系統自帶的版本。php

一、安裝 yum 源(地址從 http://yum.postgresql.org/repopackages.php 獲取)sql

yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

二、安裝PostgreSQL數據庫

這裏最核心的是要安裝postgresql96-server和postgresql96-contrib,其中」contrib」包裏包含了一些經常使用的組件和方法。centos

yum install postgresql96-server postgresql96-contrib

安裝後,可執行文件在 /usr/pgsql-9.6/bin/, 數據和配置文件在 /var/lib/pgsql/9.6/data/bash

三、初始化數據庫服務器

/usr/pgsql-9.6/bin/postgresql96-setup initdb

若是指定數據目錄執行下列命令:dom

[root@localhost ~]# sudo -i -u postgres /usr/pgsql-9.6/bin/initdb -D /data/pg/pgdata

四、默認狀況下PostgreSQL不支持密碼登陸,如需支持須要修改配置文件socket

vi /var/lib/pgsql/9.6/data/pg_hba.conf

將未註釋行中的ident 替換爲 md5ide

# TYPE  DATABASE        USER            ADDRESS                 METHOD
 
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

如需開啓遠程訪問,可編輯/var/lib/pgsql/9.6/data/postgresql.conf 文件。post

將 #listen_addresses = 'localhost' 修改成 listen_addresses='' (此處‘’也能夠改成你想開放的服務器IP)

另外對於特定的IP還能夠設置開啓信任遠程鏈接,修改/var/lib/pgsql/9.6/data/pg_hba.conf,按下面的格式進行添加設置。

# IPv4 local connections:
 host    all            all      127.0.0.1/32      trust
 host    all            all      8.8.8.8/32(須要鏈接的服務器IP)  trust

修改完配置之後不要忘了重啓服務。

五、管理服務

systemctl start postgresql-9.6 #啓動服務
systemctl restart postgresql-9.6 #重啓服務
systemctl stop postgresql-9.6 #中止服務
systemctl enable postgresql-9.6 #自動啓動

六、登陸PostgreSQL

PostgreSQL 安裝完成後,會創建一個‘postgres’用戶,用於執行PostgreSQL,數據庫中也會創建一個’postgres’用戶,若是咱們要使用PostgreSQL就必須先登陸此賬號。

sudo -i -u postgres

執行後提示符會變爲 ‘-bash-4.2$’,再運行

同構執行進入 psql 進入postgresql命令行環境。

[root[@localhost~]# sudo -i -u postgres
-bash-4.2$ psql
psql (9.6.1)
Type "help" for help.
 
postgres=#

接着能夠執行 ALTER USER postgres WITH PASSWORD '123456' 來設置postgres用戶密碼,可經過 \q 退出數據庫。

七、打開防火牆

CentOS 防火牆中內置了PostgreSQL服務,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,只需以服務方式將PostgreSQL服務開放便可。

firewall-cmd --add-service=postgresql --permanent  開放postgresql服務
firewall-cmd --reload  重載防火牆
相關文章
相關標籤/搜索