Centos 7 安裝 PostgreSQL

https://www.cnblogs.com/stulzq/p/7766409.html 再附上一個其餘安裝教程 二者結合php

1、查找最新的源html

https://yum.postgresql.org/repopackages.phplinux

這裏我選擇centos7-x86 64 右鍵 複製連接。sql

在centos系統中執行 數據庫

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

執行不成功的話 centos

linux下載插件There are no enabled repos. Run "yum repolist all" to see the repos you have.報錯解決

查看這個解決方案post

2、安裝postgresql測試

先查看postgresql源centos7

yum list | grep postgresql

咱們須要安裝的是這兩個。postgresql10-contrib postgresql10-serverspa

yum install postgresql10-contrib postgresql10-server -y

3、初始化數據庫

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

在這裏,若是在裝系統開始分配var空間足夠大則能夠繼續,若是分配var空間不夠,咱們須要更改數據目錄,在這裏,咱們假設var空間足夠大。直接開始初始化。

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

 

顯示這個表明初始化成功

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

sudo systemctl start postgresql-10
sudo systemctl enable postgresql-10.service
5、登陸postgresql並設置密碼

postgresql在安裝時默認添加用戶postgres

輸入

su - postgres
psql
進入數據庫

樣子長這樣

咱們來設置密碼:

ALTER USER postgres WITH PASSWORD '密碼';

退出:\q

備註其餘:列出全部庫\l  列出全部用戶\du 列出庫下全部表\d

 

6、默認狀況下postgresql是不用密碼不支持遠程登陸的。咱們須要修改配置文件

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

就是在最底部加入 

host    all         all         0.0.0.0/0        trust    

遠程鏈接postgresql的開啓

wq保存退出

咱們改遠程訪問

vi /var/lib/pgsql/10/data/postgresql.conf

往下拉咱們會看到

須要改爲

重啓postgresql

systemctl restart postgresql-10

 

 

 

7、登陸postgresql

postgresql在安裝時默認添加用戶postgres

輸入

su - postgres
psql
進入數據庫

 


此時已經能夠進行驗證密碼

8、遠程鏈接數據庫

咱們用pgadmin進行驗證鏈接

 

 

輸入相關信息

 

 

此時咱們能夠看到相關數據了

 

 

 

9、咱們測試建立數據庫及建立用戶

由於postgres屬於superuser,咱們須要建立部分低權限用戶

建立數據庫

CREATE DATABASE testdb;
建立用戶

CREATE USER testuser CREATEDB LOGIN PASSWORD 'testpassword';
將testdb全部權限賦給用戶testuser

GRANT ALL ON DATABASE testdb TO testuser;
樣子:

 

 

查看現有數據庫及用戶

 

 

10、刪除數據庫及測試用戶

刪除數據庫

drop database testdb;

 


刪除用戶

drop role testuser;

 


驗證:

相關文章
相關標籤/搜索