Files
chill_notes/数据库/PostgreSQL/PostgreSQL安装.md
2026-04-21 20:40:56 +08:00

92 lines
1.3 KiB
Markdown
Executable File

# PostgreSQL 安装
> CentOS 7 安装 PostgreSQL 12
---
## 安装步骤
### 1. 安装 RPM 源
```bash
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
```
### 2. 安装客户端
```bash
yum install postgresql12
```
### 3. 安装服务端
```bash
yum install postgresql12-server
```
### 4. 初始化数据库
```bash
/usr/pgsql-12/bin/postgresql-12-setup initdb
```
### 5. 启动服务
```bash
systemctl enable postgresql-12
systemctl start postgresql-12
```
---
## 配置
### 数据目录
默认数据目录:`/var/lib/pgsql/12/data/`
### 连接配置
```bash
sudo -u postgres psql
```
```sql
-- 修改 postgres 用户密码
ALTER USER postgres WITH PASSWORD '你的密码';
-- 允许远程连接
-- 编辑 pg_hba.conf
host all all 0.0.0.0/0 md5
-- 编辑 postgresql.conf
listen_addresses = '*'
```
---
## 常用命令
```bash
# 启动/停止/重启
systemctl start postgresql-12
systemctl stop postgresql-12
systemctl restart postgresql-12
# 连接数据库
psql -U postgres -h localhost
# 创建数据库
createdb mydb
# 备份
pg_dump -U postgres mydb > backup.sql
# 恢复
psql -U postgres mydb < backup.sql
```
---
> 参考:[PostgreSQL 官方安装指南](https://www.postgresql.org/download/linux/redhat/)