postgresql+centos7

一、更新源

地址:https://yum.postgresql.org/repopackages.php
打開上述地址,找到相對應的操作系統,右鍵,複製鏈接地址,執行:
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
我的系統是centos7,我複製的地址是上述地址。

二、安裝postgresql

先查看postgresql源
yum list | grep postgresql
列出postgresql
選擇想要的postgresql版本進行安裝
我們需要安裝的是這兩個。postgresql11-contrib postgresql11-server
yum install postgresql11-contrib postgresql11-server

三、初始化數據庫

Postgresql安裝目錄是/usr/pgsql-10,而Postgresql的數據目錄是/var/lib/pgsql/版本號/data目錄

在這裏,如果在裝系統開始分配var空間足夠大則可以繼續,如果分配var空間不夠,我們需要更改數據目錄,在這裏,我們假設var空間足夠大。直接開始初始化。

/usr/pgsql-11/bin/postgresql-11-setup initdb

四、啓動數據庫並設置開機啓動

sudo systemctl start postgresql-11
sudo systemctl enable postgresql-11.service

五、登錄postgresql並設置密碼

postgresql在安裝時默認添加用戶postgres
[root@localhost ~]# su - postgres
-bash-4.2$ psql
psql (11.7)
輸入 “help” 來獲取幫助信息.

修改postgres密碼:
postgres=# alter user postgres with password ‘密碼’;
ALTER ROLE
默認情況下postgresql是不用密碼不支持遠程登錄的。我們需要修改配置文件
vi /var/lib/pgsql/10/data/pg_hba.conf
將這段修改成如下:
#Allow replication connections from localhost, by a user with the
#replication privilege.
#local replication all peer
#host replication all 127.0.0.1/32 ident
#host replication all ::1/128 ident
host all all 0.0.0.0/0 md5

六、遠程訪問

vi /var/lib/pgsql/11/data/postgresql.conf
找到:listen_addresses = 'localhost’修改爲:listen_addresses = '*'去掉前面的**"#"**
找到:port = 5432去掉前面的**"#"**

七、重啓postgresql

systemctl restart postgresql-11
現在就可以通過客戶端進行遠程連接了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章