温馨提示×

centos如何导入postgresql

小樊
44
2026-09-24 16:13:24
栏目: 云计算

在 CentOS 上“导入 PostgreSQL”通常有两种理解:① 安装并启动 PostgreSQL 服务;② 把数据(SQL / 备份文件)导入到 PostgreSQL 中。下面分别说明。


一、在 CentOS 上安装 PostgreSQL(以 PostgreSQL 15 为例)

1. 添加 PostgreSQL 官方 YUM 源

sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

CentOS 7 用 EL-7,CentOS 8/Stream 用 EL-8

2. 安装 PostgreSQL 服务

sudo dnf install -y postgresql15-server postgresql15

3. 初始化数据库

sudo /usr/pgsql-15/bin/postgresql-15-setup initdb

4. 启动并设置开机自启

sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15

5. 登录测试

sudo -u postgres psql

二、将数据导入 PostgreSQL

方式 1:导入 SQL 文件

# 切换到 postgres 用户
sudo -u postgres psql

# 在 psql 中
\i /path/to/file.sql

或命令行直接导入:

sudo -u postgres psql -d 数据库名 -f /path/to/file.sql

方式 2:导入 dump 备份(pg_dump 导出)

sudo -u postgres pg_restore -d 数据库名 /path/to/file.dump

方式 3:导入 CSV 文件

COPY 表名 FROM '/path/to/file.csv' WITH (FORMAT csv, HEADER true);

三、常见问题

1. 远程无法连接

编辑:

/var/lib/pgsql/15/data/postgresql.conf
listen_addresses = '*'
/var/lib/pgsql/15/data/pg_hba.conf
host all all 0.0.0.0/0 md5

重启服务:

sudo systemctl restart postgresql-15

2. 防火墙

sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload

如果你指的是 “从 MySQL / Oracle 导入” 或 “Docker 方式部署”,可以告诉我具体场景,我可以给你更针对性的步骤。

0 踩