---
title: （可选）导入数据
description: "使用Sysbench向PostgreSQL数据库中导入数据，用于验证PostgreSQL迁移后的数据的正确性和一致性。如果实际环境已包含所需数据，请跳过本章节。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdbs/ecosystemEnable/PostgreSQL/kunpengdbs_ptgl_btps_0007.html
sourcePath: /source/zh/kunpengdbs/ecosystemEnable/PostgreSQL/kunpengdbs_ptgl_btps_0007.html
indexId: 773e49fd9c56429d40e9dc22e9f5ff4312d586edc0cbc8cd9f02d1985b01567179
---
# （可选）导入数据

使用Sysbench向PostgreSQL数据库中导入数据，用于验证PostgreSQL迁移后的数据的正确性和一致性。如果实际环境已包含所需数据，请跳过本章节。

假设需要在PostgreSQL数据库中预置数据，以10张表，每张表包含100万行为例。

1. 安装Sysbench。

  a. 获取并解压Sysbench 1.0.20源码。详细操作步骤请参见《Sysbench 0.5&1.0 测试指导》的    获取源码(https://www.hikunpeng.com/document/detail/zh/kunpengdbs/testguide/tstg/kunpengsysbench_02_0004.html)
。
  b. 在解压后的源码目录中，安装Sysbench所需的依赖。

```
yum install -y mariadb-devel postgresql-devel
```


  c. 生成自动配置脚本。

```
./autogen.sh
```


  d. 为了使Sysbench支持PostgreSQL测试，指定PostgreSQL的include目录和lib目录。

```
./configure --with-mysql --with-pgsql --with-pgsql-includes=/usr/local/pgsql-13.2/include --with-pgsql-libs=/usr/local/pgsql-13.2/lib
```


    --with-pgsql-includes=/usr/local/pgsql-13.2/include：表示编译时需要指定PostgreSQL的头文件路径。 --with-pgsql-libs=/usr/local/pgsql-13.2/lib：表示编译时需要指定PostgreSQL的库文件路径。

  e. 编译安装Sysbench。

```
make clean && make -j96 && make -j96 install
```


    “-j96”参数充分利用多核CPU优势，加快编译速度，参数“-j”后数字为CPU核数，可用cat /proc/cpuinfo | grep processor | wc -l进行查看，此数值应小于等于CPU核数。

  f. 验证已安装的Sysbench已支持PostgreSQL。

```
sysbench --db-driver --help
```


    在回显中看到pgsql - PostgreSQL driver和pgsql options的相关参数，表示已支持PostgreSQL。

2. 使用Sysbench向PostgreSQL数据库中导入数据。

  a. 切换到postgres用户。

```
su - postgres
```


  b. 创建一个名为sysbench的数据库，用于存放测试数据。

```
/usr/local/pgsql-13.2/bin/createdb sysbench
```


  c. 导入数据。

```
sysbench \
--db-driver=pgsql \
--pgsql-host=127.0.0.1 \
--pgsql-port=5432 \
--pgsql-user=postgres \
--pgsql-password=123456 \
--pgsql-db=sysbench \
--table_size=1000000 \
--tables=10 \
--time=180 \
--threads=96 \
--report-interval=10 oltp_read_write prepare
```


    上述命令会向sysbench数据库中导入数据，创建10张表，每张表包含100万行数据。
