# PostgreSQL 服务安全加固

## 一、**帐号权限**

**名称：**&#x6570;据库帐号管理

**实施目的：**&#x907F;免数据库系统用户出现漏洞被攻击

**问题影响：**&#x8D8A;权操作数据信息泄露

**实施步骤：**

1、改变默认postgres管理员帐号密码

PostgreSQL初始化后会自动生成postgres和template1库以及默认postgres管理用户，以后创建数据库会从模板数据库复制，postgres用户可以访问模板库，针对postgres用户密码使用复杂密码进行加密保存。

2、关于密码的管理

使用加密函数针对数据库用户口令加密

## **二、修改默认服务端口**

**名称：**&#x66F4;改PostgreSQL服务器默认端口

**实施目的：**&#x66F4;改PostgreSQL服务器默认端口,增加系统安全性

**问题影响：**&#x4E0D;安全性增加

**实施步骤：**

1、参考配置操作

（1）修改`$PGDATA/postgresql.conf`配置文件，更改默认管理端口到8412

```
port=8412
```

（2）重启postgresql服务

## **三、设置信任IP连接监听器**

**名称：**&#x66F4;改PostgreSQL服务器默认监听端口

**实施目的：**&#x66F4;改PostgreSQL服务器默认监听端口,增加系统安全性

**问题影响：**&#x4E0D;安全性增加

**实施步骤：**

（1）修改`$PGDATA/postgresql.conf`配置文件，只监听受信任的IP地址

```
listen_addresses='192.168.180.25'
```

（2）重启postgresql服务

## **四、 强密码策略**

**名称：**&#x4F7F;用PostgreSQL服务器的强密码策略

**实施目的：**&#x66F4;改PostgreSQL使用服务器的强密码策略,增加系统安全性

**问题影响：**&#x4E0D;安全性增加

**实施步骤：**

（1）编译安装`contrib/passwordcheck`

```
Make; make install
```

（2）修改`$PGDATA/postgresql.conf`配置文件，添加

```
shared_preload_libraries='passwordcheck'
```

（3）重启postgresql服务

## **五、安全审计**

**名称：**&#x542F;用日志记录功能

**实施目的：**&#x914D;置日志功能，对用户请求的SQL进行记录，记录内容包括用户、操作时间、执行SQL等。

**问题影响：**&#x975E;法访问、恶意攻击

**实施步骤：**

（1）修改`$PGDATA/postgresql.conf`配置文件，配置记录的日志级别

```
log_statement='mod'#none,ddl,mod,all
```

（2）重启postgresql服务

## **六、 漏洞补丁**

**名称：**&#x8865;丁修复

**实施目的：**&#x5347;级PostgreSQL修复漏洞

**问题影响：**&#x5BB9;易引起恶意攻击。

**实施步骤：**&#x5230;[PostgreSQL 官网](https://www.postgresql.org/support/security/) 查看安全问题，到[PostgreSQL 官网源站](https://www.postgresql.org/ftp/source/) 下载对应的版本进行修复

## **七、数据访问权限控制**

**名称：**&#x9650;定用户的数据访问权限

**实施目的：**&#x50;ostgreSQL权限系统的主要功能是验证登陆用户，并且赋予该用户在数据库上的SELECT、INSERT、UPDATE和DELETE等权限,管理员可以对user，db，host,schemas,tables,columns等数据库对象进行配置，来控制用户的访问权限。

* user：访问数据库的用户
* db：用户要访问的数据库
* host：用户能访问的主机
* schemas：用户能访问的schema
* tables：用户能访问的表
* columns：用户能访问的表的列

针对不同的用户类型采用最小访问权限的授权方式将会提升数据库的安全级别

**问题影响：**&#x9020;成用户非法越权访问

**实施步骤：**&#x6839;据业务需求和安全需求，通过PostgreSQL命令授予给用户数据库、方案、表、列甚至存储过程的操作权限。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/xi-tong-an-quan-jia-gu/postgresql-fu-wu-an-quan-jia-gu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
