一、PostgreSQL 下載安裝
1、安裝PostgreSQL倉庫(添加yum源)
1
2
3
|
[root@localhost ~]
# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # 方式一
[root@localhost ~]
# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm # 方式二
[root@localhost ~]
# yum install -y https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # 方式三
|
2、 安裝PostgreSQL服務端
1
2
3
|
[root@localhost ~]
# yum install -y postgresql12 (客戶端安裝)
[root@localhost ~]
# yum install -y postgresql12-server (服務端安裝)
[root@localhost ~]
# yum install -y postgresql12 postgresql12-server postgresql12-contrib (一步到位,相當於上面兩步)
|
3、初始化數據庫:使用默認數據目錄,yum安裝的postgresql的默認數據目錄為 /var/lib/pgsql/12/data,直接初始化就行。
1
|
[root@localhost ~]
# /usr/pgsql-12/bin/postgresql-12-setup initdb
|
4、初始化數據庫:使用指定數據目錄,創建目錄、增加用戶、給用戶賦權、初始化數據庫。與步驟3選擇其中一個即可。
1
2
3
4
5
6
7
8
9
10
|
[root@localhost ~]
# mkdir -p /data/postgresql/data # 新建數據目錄
[root@localhost ~]
# useradd postgres # 創建用戶
[root@localhost ~]
# chown -R postgres:postgres /data/postgresql/data # 授權,給創建的目錄賦予 postgres 用戶權限
[root@localhost ~]
# su - postgres # 切換到postgres用戶
[root@localhost ~]
# /usr/pgsql-12/bin/initdb -D /data/postgresql/data # 初始化數據庫
[root@localhost ~]
# vim /usr/lib/systemd/system/postgresql-12.service # 修改postgresql的system.service配置文件:修改如下
Environment=PGDATA=
/data/postgresql/data/
[root@localhost ~]
# systemctl daemon-reload # 重新加載系統服務
|
5、啟動 PostgreSQL 服務
1
2
3
|
[root@localhost ~]
# systemctl enable postgresql-12
[root@localhost ~]
# systemctl start postgresql-12
[root@localhost ~]
# systemctl status postgresql-12
|
postgresql會自動完成以下操作:
- 自動生成一個linux系統用戶postgres:管理數據庫的系統用戶。
- 數據庫用戶postgres:數據庫超級管理員,此用戶的默認數據庫為postgres。
6、修改數據庫賬戶postgres默認密碼
1
2
3
|
[root@localhost ~]
# su - postgres # 或者:sudo -i -u postgres
[postgres@localhost ~]
# psql # 進入postgresql命令行
postgres=
# alter user postgres password '123456' # alter role postgres with password '123456';
|
7、修改配置文件允許遠程連接
1
2
3
4
5
6
7
8
|
[postgres@localhost ~]
# vim /data/postgresql/data/postgresql.conf # 使用指定數據目錄
[postgres@localhost ~]
# vim /var/lib/pgsql/12/data/postgresql.conf # 使用默認數據目錄
listen_addresses =
'*'
# 修改監聽的ip
port = 5432
# 修改監聽的端口
[postgres@localhost ~]
# vim /data/postgresql/data/pg_hba.conf # 使用指定數據目錄
[postgres@localhost ~]
# vim /var/lib/pgsql/12/data/pg_hba.conf # 使用默認數據目錄
host all all 0.0.0.0
/0
password
# 所有的用戶通過任意IP都可以使用面膜的方式登錄PostgreSQL
|
8、數據庫服務器開啟、重啟和狀態查看
1
2
3
4
5
6
|
[root@localhost ~]
# systemctl stop postgresql-12 # 停止服務
[root@localhost ~]
# systemctl start postgresql-12 # 啟動服務
[root@localhost ~]
# systemctl status postgresql-12 # 服務狀態
[root@localhost ~]
# systemctl restart postgresql-12 # 重啟服務
[root@localhost ~]
# netstat -lnput # 服務狀態
|
二、客戶端連接工具下載:
Navicat:http://www.navicat.com.cn/download/navicat-for-postgresql
pgAdmin:https://www.pgadmin.org/download/
二、PostgreSQL 配置文件
三、PostgreSQL 常用命令
1、安裝完畢后,系統會創建一個數據庫超級用戶 postgres,密碼為空。首先切換到 postgres 用戶。
1
|
[root@localhost ~]
# sudo -i -u postgres
|
2、使用 psql 命令進入 postgres 命令行。
1
|
[postgres@localhost ~]
# psql
|
3、輸入 exit 命令退出 postgres 命令行。
1
2
|
postgres=
# \q # 退出方式一
postgres=
# exit # 退出方式二
|
4、遠程連接訪問 postgresql 數據庫
1
2
3
4
|
[root@localhost ~]
# psql -h IPadrress -p 1921 -U username -d database
[root@localhost ~]
# psql -h localhost -p 5432 -U postgress runoobdb # 5432:默認端口,可以指定端口如:1921。
[root@localhost ~]
# psql -h localhost -p 5432 -U postgress -d runoobdb # runoobdb:數據庫名稱(可以省略-d選項)。
[root@localhost ~]
# psql -h pgm-uf6mix.pg.rds.aliyuncs.com -p 1921 -U postgres -d shop # 1921:指定端口,postgres:用戶名,shop:數據庫名稱。
|
5、
https://www.icode9.com/content-2-729145.html
https://blog.csdn.net/wc1695040842/article/details/106780666
https://shixiongfei.com/centos-install-postgresql-timescaledb.html
https://blog.csdn.net/weixin_42290927/article/details/112476306
https://blog.csdn.net/weixin_42514606/article/details/87565307