在 CentOS 上导入 PostgreSQL 数据,常见有几种方式,取决于你的数据来源(SQL 文件、dump 文件、CSV 等)。下面按最常用场景说明。
sudo yum install -y postgresql
如果你用的是 PostgreSQL 12+:
sudo dnf install -y postgresql
systemctl status postgresql
切换为 postgres 用户:
sudo -u postgres psql
psql -U username -d dbname -h localhost -f /path/to/file.sql
示例:
psql -U postgres -d testdb -f /home/user/data.sql
如果是 postgres 用户本地导入:
sudo -u postgres psql -d testdb -f /home/user/data.sql
\i /path/to/file.sql
pg_restore -U postgres -d testdb /path/to/backup.dump
psql -U postgres -d testdb -f backup.sql
假设表结构已存在:
COPY table_name FROM '/path/to/file.csv' WITH CSV HEADER;
⚠️ 注意:
postgres 用户读取/tmp 或数据库可访问目录sudo -u postgres createdb testdb
sudo -u postgres psql -d testdb -f data.sql
chown postgres:postgres file.sql
chmod 600 file.sql
export PGCLIENTENCODING=UTF8
psql -h 192.168.1.10 -U postgres -d dbname -f file.sql
如果你能说明:
我可以给你更精确的命令。