使用C#将数据从CSV文件批量复制到PostgreSQL数据库

时间:2020-10-06 22:32:36

标签: c# postgresql csv amazon-s3

我有一个CSV文件,其中包含我需要复制到云中Postgres数据库中的表的数据。 我可以从C#控制台应用程序执行此操作吗?如果可以的话,语法是什么?到目前为止,我发现的所有内容都没有给我一个清晰的示例,说明如何使用C#进行调用。在我的应用程序中,我正在使用与Postgres数据库的DSN连接(PostgreSQL ODBC驱动程序)。 CSV文件可以是本地文件,也可以在S3存储桶中。

这是我的代码(略有简化):

string filePath = "C:\Bluh\Data.CSV";
string sql = string.Format("COPY {0}.{1} FROM '{2}' DELIMITER ',' CSV HEADER ", postgres_catalog, stagingTableName, filePath);
using (OdbcCommand command = new OdbcCommand(sql))
{
   OdbcConnection SQLConn = new OdbcConnection(DBConnectionString);
   SQLConn.Open();
   sc.Connection = SQLConn;
   command.ExecuteNonQuery();
}

使用上面的代码时出现错误:

ERROR [42501] ERROR: must be superuser or a member of the pg_read_server_files role to COPY from a file;

如果无法在本地文件上使用COPY命令,如何在S3存储桶中指定该文件的路径?语法是什么?

干杯!

1 个答案:

答案 0 :(得分:0)

这是我发现的东西,对我有用

const getCreators = () => {
  return window
    .fetch(url + '/get-creators', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    })
    .then((res) => {
      console.log(res);
      if (res.status === 200) {
        return res.json();
      } else {
        return null;
      }
    })
    .then((data) => {
      if (!data || data.error) {
        return null;
      } else {
        return data;
      }
    });
};

确保您在DSN中使用的帐户具有足够的特权。

还请注意,文件名区分大小写,包括扩展名

相关问题