且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 C# 数据集批量插入 AWS PostgreSql

更新时间:2023-12-04 15:51:10

根据我的评论和您的后续问题,如果您从文件中复制 Npgsql,那么您需要超级用户权限.这是因为该文件将驻留在 PostgreSQL 服务器上,人们会认为很少有用户(希望只有管理员)可以访问甚至知道.

Following up on my comment and your subsequent question, if you do a Npgsql copy from a file, then you need superuser rights. This is because the file will reside on the PostgreSQL server, which one would think very few users (hopefully only admins) have access to or even know about.

但是,如果您从 STDIN 复制 - 标准输入,那么这本质上就是您将要提供的数据流.

However, if you copy from STDIN - standard input, then that is essentially a stream of data that you will then feed.

关于如何从 STDIN 复制的非常简短的入门/示例如下:

A very brief primer/example on how to copy from STDIN follows:

using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
{
    conn.Open();

    using (var writer = conn.BeginBinaryImport("copy my_table from STDIN (FORMAT BINARY)"))
    {
        foreach (object[] fields in myData)
        {
            writer.StartRow();
            writer.Write(fields[0]); // Assumes varchar
            writer.Write(fields[1], NpgsqlTypes.NpgsqlDbType.Integer);
            writer.Write(fields[2], NpgsqlTypes.NpgsqlDbType.Date);
        }

        writer.Complete();
    }
}

更多关于 Npgsql 帮助站点的信息.

Much more on the Npgsql help site.