I'm creating .csv files in my C# program and I want to upload them into a postgres table via COPY command. COPY wants the files to be on database server but I have them localy. Is there a way to upload them from the local machine?
Talk1:
psql \copy lets you upload from client to server.
Solutions1
If you can access to the server's hard drive, you can just copy it with File.Copy method :
File.Copy(sourceFile, destFile);
Talk1:
Well it looks like your question is more a postgresql than a C# question then.
Solutions2
I think the best answer is: "Yes, there are many ways to upload file to remote server".
You have really many options to do this. One that you may prefer depends on the way you communicate with your server.
For example, you can upload your file via FTP or submit it with web form through HTTP.
Or you can attach remote filesystem as external drive to your local machine and just copy that.
Also, there are many utilities like SqlBulkCopy. It allows you not to upload your file but remotely insert multiple rows from DataTable.
Solutions3
Well I found very clever way to do it :)
I'm using STDIN in my query and after that I'm streaming the contents of the file...
NpgsqlCommand dbcmd = dbcon.CreateCommand();
string sql =
"COPY table_name FROM STDIN;";
dbcmd.CommandText = sql;
serializer = new NpgsqlCopySerializer(dbcon);
copyIn = new NpgsqlCopyIn(dbcmd, dbcon, serializer.ToStream);
copyIn.Start();
foreach(...){
serializer.Add...
serializer.EndRow();
serializer.Flush();
}
copyIn.End();
serializer.Close();
Talk1:
I get serializer.ToStream is not set to an instance of an object