public inbox for [email protected]  
help / color / mirror / Atom feed
From: gzh <[email protected]>
To: [email protected]
Subject: Re:Question Regarding COPY Command Handling of Line Breaks in PostgreSQL
Date: Wed, 16 Jul 2025 21:02:52 +0800 (CST)
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>




The cause has been identified. In my VB program, I was using the COPY command. 

To prevent memory issues with large files, I changed the file reading method from reading the entire file at once to reading it line by line, as shown below. 

However, this caused line breaks within the data to be lost. This is not an issue with the COPY command itself. 

I tested reading the entire file at once, and it worked without any problems. My apologies for the confusion.




Reading the entire file at once:




Using reader As New StreamReader(inputFile, System.Text.Encoding.UTF8)

    Using copyIn = conn.BeginTextImport("COPY Schema.tableName FROM STDIN WITH CSV HEADER")

        copyIn.Write(reader.ReadToEnd())

    End Using

End Using




Reading the file line by line:




Using reader As New StreamReader(inputFile, System.Text.Encoding.UTF8)

    Using copyIn = conn.BeginTextImport("COPY Schema.tableName FROM STDIN WITH CSV HEADER")

        While Not reader.EndOfStream

            Dim line As String = reader.ReadLine()

            copyIn.Write(line & vbLf)

        End While

    End Using

End Using













At 2025-07-11 20:20:38, "gzh" <[email protected]> wrote:

Dear,


I am encountering an issue with the COPY command in PostgreSQL regarding the handling of line breaks in data fields. My PostgreSQL instance is installed on a Linux system.


When I use the COPY command to export data to a CSV file and then import the same CSV file back into the database, all carriage return characters (CR) in the fields are automatically converted to line feed characters (LF). For example:


Field content before import: *** (CR) ***
Field content after import: *** (LF) ***
I would like to know if there is a way to use the COPY command while preserving the original line breaks (CR) in the data fields during import.


Thank you for your assistance.


Best regards,
gzh

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re:Question Regarding COPY Command Handling of Line Breaks in PostgreSQL
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox