Re:Question Regarding COPY Command Handling of Line Breaks in PostgreSQL

From: gzh <gzhcoder(at)126(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re:Question Regarding COPY Command Handling of Line Breaks in PostgreSQL
Date: 2025-07-16 13:02:52
Message-ID: 29948df8.8c95.1981354aa58.Coremail.gzhcoder@126.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

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" <gzhcoder(at)126(dot)com> 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Amol Inamdar 2025-07-16 13:24:50 Re: Bypassing Directory Ownership Check in PostgreSQL 16.6 with Secure z/OS NFS (AT-TLS)
Previous Message Achilleas Mantzios 2025-07-16 08:30:03 Re: PgBouncer-Postgres : un supported startup parameter statement_timeout