> ## Content Index
> Fetch the complete content index at: https://note.xdq.me/llms.txt
> Use this file to discover other available public pages before exploring further.

# centos安装postgresql并配置
- URL: https://note.xdq.me/centosan-zhuang-postgresql/
- Published: 2015-03-28T11:40:51.000Z
- Updated: 2026-10-03T03:06:37.000Z
- Author: xud6
- Tags: centos, postgresql, #Import 2026-10-03 02:45

##### 安装postgresql

```
yum install -y postgresql-server

```

###### 初始化数据库

```
postgresql-setup initdb

```

###### 允许外部访问数据库

```
nano /var/lib/pgsql/data/postgresql.conf

```

替换`listen_addresses = 'localhost'`为`listen_addresses = '*'`

###### 允许通过密码登录

```
nano /var/lib/pgsql/data/pg_hba.conf

```

将ident替换为md5

```
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5	

```

如果需要外部访问添加

```
host    all             all             107.155.94.0/24         md5

```

###### 启动服务器并开启自动启动

```
systemctl start postgresql.service
systemctl enable postgresql.service

```

###### 打开防火墙5432端口

##### 创建数据库和用户

```
su - postgres
createdb test
psql test
CREATE USER testusr WITH SUPERUSER LOGIN PASSWORD 'testusr';

```

###### 测试

```
psql -h localhost -d test -U testusr

```

##### 管理员远程访问密码

```
su - postgres
psql postgres

```

然后

```
\password postgres

```