info > COPY

The user wants me to transform a man page (PostgreSQL COPY documentation) into an emoji-rich HTML version. Let me carefully follow all the rules: 1. NEVER use

— start from

2. Section titles →

with ONE emoji prefix 3. Sub-sections →

with emoji prefix 4. Comments in code are NOT headings 5. ALL code MUST be in
...
6. Code blocks contain EXACT original code — no emoji inside 7. Use proper HTML lists 8. Function references use
  • name(args) — description
  • 9. Escape < and > as < and > outside allowed tags 10. Output only valid HTML 11. Preserve original HTML structure 12. Add 🚀 Quick Reference section as second section 13. Add Exit Codes section only if explicitly listed (not here) 14. Keep under 128,000 characters Let me organize the content: - NAME section →

    📛 NAME

    - Quick Reference →

    🚀 Quick Reference

    - SYNOPSIS →

    📋 SYNOPSIS

    - DESCRIPTION →

    📖 DESCRIPTION

    - PARAMETERS →

    ⚙️ PARAMETERS

    - OUTPUTS →

    📤 OUTPUTS

    - NOTES →

    📝 NOTES

    - FILE FORMATS →

    📂 FILE FORMATS

    - Text Format →

    📄 Text Format

    - CSV Format →

    📊 CSV Format

    - Binary Format →

    🔢 Binary Format

    - File Header →

    📋 File Header

    - Signature →

    🖋️ Signature

    ... wait, the rules say start from h2. But there's clearly nesting here. Let me use h3 for subsections and h4 for sub-subsections. Actually, re-reading: "Sub-sections within a section →

    with emoji prefix". So h3 for sub-sections. For further nesting, h4 makes sense. Let me also handle the table with backslash sequences - that's already a table in the original, so I'll preserve it. Let me also handle the binary format od -c output which has special chars. The "Note" blocks in the original are styled with indentation. I'll use
    for them. Let me write the HTML now. I need to be careful with the special characters in the binary format output (those are octal/control characters). The od -c output has characters like \b, \n, \r, etc. - these should be preserved exactly inside
    .
    
    The "gzip >" in the example - the > is already escaped.
    
    Let me also note that the original contains  tags which I should preserve outside of code blocks.
    
    Let me start writing:
    
    ```html
    

    📛 NAME

    COPY — copy data between a file and a table

    🚀 Quick Reference

    Use CaseCommandDescription
    Copy table to fileCOPY table TO '/path/file.csv'Export table data to a file
    Copy file to tableCOPY table FROM '/path/file.csv'Import data from a file
    Copy to clientCOPY table TO STDOUTOutput data to client application
    Copy from clientCOPY table FROM STDINRead input from client application
    Copy query resultsCOPY (SELECT * FROM t WHERE ...) TO '/path/file'Export filtered query results
    CSV formatCOPY table TO '/path/file' WITH (FORMAT csv, HEADER true)Export in CSV format with header
    Compressed exportCOPY table TO PROGRAM 'gzip > file.gz'Pipe output through external program
    Filtered importCOPY table FROM '/path/file' WHERE col > 0Import rows matching condition

    📋 SYNOPSIS

    COPY table_name [ ( column_name [, ...] ) ]
        FROM { 'filename' | PROGRAM 'command' | STDIN }
        [ [ WITH ] ( option [, ...] ) ]
        [ WHERE condition ]
    
    COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
        TO { 'filename' | PROGRAM 'command' | STDOUT }
        [ [ WITH ] ( option [, ...] ) ]
    
    where option can be one of:
    
        FORMAT format_name
        FREEZE [ boolean ]
        DELIMITER 'delimiter_character'
        NULL 'null_string'
        HEADER [ boolean ]
        QUOTE 'quote_character'
        ESCAPE 'escape_character'
        FORCE_QUOTE { ( column_name [, ...] ) | * }
        FORCE_NOT_NULL ( column_name [, ...] )
        FORCE_NULL ( column_name [, ...] )
        ENCODING 'encoding_name'
    

    📖 DESCRIPTION

    COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already). COPY TO can also copy the results of a SELECT query.

    If a column list is specified, COPY TO copies only the data in the specified columns to the file. For COPY FROM, each field in the file is inserted, in order, into the specified column. Table columns not specified in the COPY FROM column list will receive their default values.

    COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible by the PostgreSQL user (the user ID the server runs as) and the name must be specified from the viewpoint of the server. When PROGRAM is specified, the server executes the given command and reads from the standard output of the program, or writes to the standard input of the program. The command must be specified from the viewpoint of the server, and be executable by the PostgreSQL user. When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server.

    Each backend running COPY will report its progress in the pg_stat_progress_copy view. See Section 28.4.6 for details.

    ⚙️ PARAMETERS

    table_name

    The name (optionally schema-qualified) of an existing table.

    column_name

    An optional list of columns to be copied. If no column list is specified, all columns of the table except generated columns will be copied.

    query

    A SELECT, VALUES, INSERT, UPDATE, or DELETE command whose results are to be copied. Note that parentheses are required around the query.

    For INSERT, UPDATE and DELETE queries a RETURNING clause must be provided, and the target relation must not have a conditional rule, nor an ALSO rule, nor an INSTEAD rule that expands to multiple statements.

    filename

    The path name of the input or output file. An input file name can be an absolute or relative path, but an output file name must be an absolute path. Windows users might need to use an E'' string and double any backslashes used in the path name.

    PROGRAM

    A command to execute. In COPY FROM, the input is read from standard output of the command, and in COPY TO, the output is written to the standard input of the command.

    Note that the command is invoked by the shell, so if you need to pass any arguments to shell command that come from an untrusted source, you must be careful to strip or escape any special characters that might have a special meaning for the shell. For security reasons, it is best to use a fixed command string, or at least avoid passing any user input in it.

    STDIN

    Specifies that input comes from the client application.

    STDOUT

    Specifies that output goes to the client application.

    boolean

    Specifies whether the selected option should be turned on or off. You can write TRUE, ON, or 1 to enable the option, and FALSE, OFF, or 0 to disable it. The boolean value can also be omitted, in which case TRUE is assumed.

    FORMAT

    Selects the data format to be read or written: text, csv (Comma Separated Values), or binary. The default is text.

    FREEZE

    Requests copying the data with rows already frozen, just as they would be after running the VACUUM FREEZE command. This is intended as a performance option for initial data loading. Rows will be frozen only if the table being loaded has been created or truncated in the current subtransaction, there are no cursors open and there are no older snapshots held by this transaction. It is currently not possible to perform a COPY FREEZE on a partitioned table.

    Note that all other sessions will immediately be able to see the data once it has been successfully loaded. This violates the normal rules of MVCC visibility and users specifying should be aware of the potential problems this might cause.

    DELIMITER

    Specifies the character that separates columns within each row (line) of the file. The default is a tab character in text format, a comma in CSV format. This must be a single one-byte character. This option is not allowed when using binary format.

    NULL

    Specifies the string that represents a null value. The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format. You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings. This option is not allowed when using binary format.

    Note: When using COPY FROM, any data item that matches this string will be stored as a null value, so you should make sure that you use the same string as you used with COPY TO.

    HEADER

    Specifies that the file contains a header line with the names of each column in the file. On output, the first line contains the column names from the table, and on input, the first line is ignored. This option is allowed only when using CSV format.

    QUOTE

    Specifies the quoting character to be used when a data value is quoted. The default is double-quote. This must be a single one-byte character. This option is allowed only when using CSV format.

    ESCAPE

    Specifies the character that should appear before a data character that matches the QUOTE value. The default is the same as the QUOTE value (so that the quoting character is doubled if it appears in the data). This must be a single one-byte character. This option is allowed only when using CSV format.

    FORCE_QUOTE

    Forces quoting to be used for all non-NULL values in each specified column. NULL output is never quoted. If * is specified, non-NULL values will be quoted in all columns. This option is allowed only in COPY TO, and only when using CSV format.

    FORCE_NOT_NULL

    Do not match the specified columns' values against the null string. In the default case where the null string is empty, this means that empty values will be read as zero-length strings rather than nulls, even when they are not quoted. This option is allowed only in COPY FROM, and only when using CSV format.

    FORCE_NULL

    Match the specified columns' values against the null string, even if it has been quoted, and if a match is found set the value to NULL. In the default case where the null string is empty, this converts a quoted empty string into NULL. This option is allowed only in COPY FROM, and only when using CSV format.

    ENCODING

    Specifies that the file is encoded in the encoding_name. If this option is omitted, the current client encoding is used. See the Notes below for more details.

    WHERE

    The optional WHERE clause has the general form

    WHERE condition
    

    where condition is any expression that evaluates to a result of type boolean. Any row that does not satisfy this condition will not be inserted to the table. A row satisfies the condition if it returns true when the actual row values are substituted for any variable references.

    Currently, subqueries are not allowed in WHERE expressions, and the evaluation does not see any changes made by the COPY itself (this matters when the expression contains calls to VOLATILE functions).

    📤 OUTPUTS

    On successful completion, a COPY command returns a command tag of the form

    COPY count
    

    The count is the number of rows copied.

    Note: psql will print this command tag only if the command was not COPY ... TO STDOUT, or the equivalent psql meta-command \copy ... to stdout. This is to prevent confusing the command tag with the data that was just printed.

    📝 NOTES

    • 📋 COPY TO can be used only with plain tables, not views, and does not copy rows from child tables or child partitions. For example, COPY table TO copies the same rows as SELECT * FROM ONLY table. The syntax COPY (SELECT * FROM table) TO ... can be used to dump all of the rows in an inheritance hierarchy, partitioned table, or view.
    • 📥 COPY FROM can be used with plain, foreign, or partitioned tables or with views that have INSTEAD OF INSERT triggers.
    • 🔐 You must have select privilege on the table whose values are read by COPY TO, and insert privilege on the table into which values are inserted by COPY FROM. It is sufficient to have column privileges on the column(s) listed in the command.
    • 🛡️ If row-level security is enabled for the table, the relevant SELECT policies will apply to COPY table TO statements. Currently, COPY FROM is not supported for tables with row-level security. Use equivalent INSERT statements instead.
    • 📁 Files named in a COPY command are read or written directly by the server, not by the client application. Therefore, they must reside on or be accessible to the database server machine, not the client. They must be accessible to and readable or writable by the PostgreSQL user (the user ID the server runs as), not the client. Similarly, the command specified with PROGRAM is executed directly by the server, not by the client application, must be executable by the PostgreSQL user. COPY naming a file or command is only allowed to database superusers or users who are granted one of the roles pg_read_server_files, pg_write_server_files, or pg_execute_server_program, since it allows reading or writing any file or running a program that the server has privileges to access.
    • 🔄 Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.
    • 🛣️ It is recommended that the file name used in COPY always be specified as an absolute path. This is enforced by the server in the case of COPY TO, but for COPY FROM you do have the option of reading from a file specified by a relative path. The path will be interpreted relative to the working directory of the server process (normally the cluster's data directory), not the client's working directory.
    • 🔒 Executing a command with PROGRAM might be restricted by the operating system's access control mechanisms, such as SELinux.
    • ⚡ COPY FROM will invoke any triggers and check constraints on the destination table. However, it will not invoke rules.
    • 🆔 For identity columns, the COPY FROM command will always write the column values provided in the input data, like the INSERT option OVERRIDING SYSTEM VALUE.
    • 📅 COPY input and output is affected by DateStyle. To ensure portability to other PostgreSQL installations that might use non-default DateStyle settings, DateStyle should be set to ISO before using COPY TO. It is also a good idea to avoid dumping data with IntervalStyle set to sql_standard, because negative interval values might be misinterpreted by a server that has a different setting for IntervalStyle.
    • 🌐 Input data is interpreted according to ENCODING option or the current client encoding, and output data is encoded in ENCODING or the current client encoding, even if the data does not pass through the client but is read from or written to a file directly by the server.
    • ⛔ COPY stops operation at the first error. This should not lead to problems in the event of a COPY TO, but the target table will already have received earlier rows in a COPY FROM. These rows will not be visible or accessible, but they still occupy disk space. This might amount to a considerable amount of wasted disk space if the failure happened well into a large copy operation. You might wish to invoke VACUUM to recover the wasted space.
    • 🔀 FORCE_NULL and FORCE_NOT_NULL can be used simultaneously on the same column. This results in converting quoted null strings to null values and unquoted null strings to empty strings.

    📂 FILE FORMATS

    📄 Text Format

    When the text format is used, the data read or written is a text file with one line per table row. Columns in a row are separated by the delimiter character. The column values themselves are strings generated by the output function, or acceptable to the input function, of each attribute's data type. The specified null string is used in place of columns that are null. COPY FROM will raise an error if any line of the input file contains more or fewer columns than are expected.

    End of data can be represented by a single line containing just backslash-period (\.). An end-of-data marker is not necessary when reading from a file, since the end of file serves perfectly well; it is needed only when copying data to or from client applications using pre-3.0 client protocol.

    Backslash characters (\) can be used in the COPY data to quote data characters that might otherwise be taken as row or column delimiters. In particular, the following characters must be preceded by a backslash if they appear as part of a column value: backslash itself, newline, carriage return, and the current delimiter character.

    The specified null string is sent by COPY TO without adding any backslashes; conversely, COPY FROM matches the input against the null string before removing backslashes. Therefore, a null string such as \N cannot be confused with the actual data value \N (which would be represented as \\N).

    The following special backslash sequences are recognized by COPY FROM:

    SequenceRepresents
    \bBackspace (ASCII 8)
    \fForm feed (ASCII 12)
    \nNewline (ASCII 10)
    \rCarriage return (ASCII 13)
    \tTab (ASCII 9)
    \vVertical tab (ASCII 11)
    \digitsBackslash followed by one to three octal digits specifies the byte with that numeric code
    \xdigitsBackslash x followed by one or two hex digits specifies the byte with that numeric code

    Presently, COPY TO will never emit an octal or hex-digits backslash sequence, but it does use the other sequences listed above for those control characters.

    Any other backslashed character that is not mentioned in the above table will be taken to represent itself. However, beware of adding backslashes unnecessarily, since that might accidentally produce a string matching the end-of-data marker (\.) or the null string (\N by default). These strings will be recognized before any other backslash processing is done.

    It is strongly recommended that applications generating COPY data convert data newlines and carriage returns to the \n and \r sequences respectively. At present it is possible to represent a data carriage return by a backslash and carriage return, and to represent a data newline by a backslash and newline. However, these representations might not be accepted in future releases. They are also highly vulnerable to corruption if the COPY file is transferred across different machines (for example, from Unix to Windows or vice versa).

    All backslash sequences are interpreted after encoding conversion. The bytes specified with the octal and hex-digit backslash sequences must form valid characters in the database encoding.

    COPY TO will terminate each row with a Unix-style newline (\n). Servers running on Microsoft Windows instead output carriage return/newline (\r\n), but only for COPY to a server file; for consistency across platforms, COPY TO STDOUT always sends \n regardless of server platform. COPY FROM can handle lines ending with newlines, carriage returns, or carriage return/newlines. To reduce the risk of error due to un-backslashed newlines or carriage returns that were meant as data, COPY FROM will complain if the line endings in the input are not all alike.

    📊 CSV Format

    This format option is used for importing and exporting the Comma Separated Value (CSV) file format used by many other programs, such as spreadsheets. Instead of the escaping rules used by PostgreSQL's standard text format, it produces and recognizes the common CSV escaping mechanism.

    The values in each record are separated by the DELIMITER character. If the value contains the delimiter character, the QUOTE character, the NULL string, a carriage return, or line feed character, then the whole value is prefixed and suffixed by the QUOTE character, and any occurrence within the value of a QUOTE character or the ESCAPE character is preceded by the escape character. You can also use FORCE_QUOTE to force quotes when outputting non-NULL values in specific columns.

    The CSV format has no standard way to distinguish a NULL value from an empty string. PostgreSQL's COPY handles this by quoting. A NULL is output as the NULL parameter string and is not quoted, while a non-NULL value matching the NULL parameter string is quoted. For example, with the default settings, a NULL is written as an unquoted empty string, while an empty string data value is written with double quotes (""). Reading values follows similar rules. You can use FORCE_NOT_NULL to prevent NULL input comparisons for specific columns. You can also use FORCE_NULL to convert quoted null string data values to NULL.

    Because backslash is not a special character in the CSV format, \., the end-of-data marker, could also appear as a data value. To avoid any misinterpretation, a \. data value appearing as a lone entry on a line is automatically quoted on output, and on input, if quoted, is not interpreted as the end-of-data marker. If you are loading a file created by another application that has a single unquoted column and might have a value of \., you might need to quote that value in the input file.

    Note: In CSV format, all characters are significant. A quoted value surrounded by white space, or any characters other than DELIMITER, will include those characters. This can cause errors if you import data from a system that pads CSV lines with white space out to some fixed width. If such a situation arises you might need to preprocess the CSV file to remove the trailing white space, before importing the data into PostgreSQL.

    Note: CSV format will both recognize and produce CSV files with quoted values containing embedded carriage returns and line feeds. Thus the files are not strictly one line per table row like text-format files.

    Note: Many programs produce strange and occasionally perverse CSV files, so the file format is more a convention than a standard. Thus you might encounter some files that cannot be imported using this mechanism, and COPY might produce files that other programs cannot process.

    🔢 Binary Format

    The binary format option causes all data to be stored/read as binary format rather than as text. It is somewhat faster than the text and CSV formats, but a binary-format file is less portable across machine architectures and PostgreSQL versions. Also, the binary format is very data type specific; for example it will not work to output binary data from a smallint column and read it into an integer column, even though that would work fine in text format.

    The binary file format consists of a file header, zero or more tuples containing the row data, and a file trailer. Headers and data are in network byte order.

    Note: PostgreSQL releases before 7.4 used a different binary file format.

    📋 File Header

    The file header consists of 15 bytes of fixed fields, followed by a variable-length header extension area. The fixed fields are:

    🖋️ Signature

    11-byte sequence PGCOPY\n\377\r\n\0 — note that the zero byte is a required part of the signature. (The signature is designed to allow easy identification of files that have been munged by a non-8-bit-clean transfer. This signature will be changed by end-of-line-translation filters, dropped zero bytes, dropped high bits, or parity changes.)

    🚩 Flags field

    32-bit integer bit mask to denote important aspects of the file format. Bits are numbered from 0 (LSB) to 31 (MSB). Note that this field is stored in network byte order (most significant byte first), as are all the integer fields used in the file format. Bits 16-31 are reserved to denote critical file format issues; a reader should abort if it finds an unexpected bit set in this range. Bits 0-15 are reserved to signal backwards-compatible format issues; a reader should simply ignore any unexpected bits set in this range. Currently only one flag bit is defined, and the rest must be zero:

    🔢 Bit 16

    If 1, OIDs are included in the data; if 0, not. Oid system columns are not supported in PostgreSQL anymore, but the format still contains the indicator.

    📏 Header extension area length

    32-bit integer, length in bytes of remainder of header, not including self. Currently, this is zero, and the first tuple follows immediately. Future changes to the format might allow additional data to be present in the header. A reader should silently skip over any header extension data it does not know what to do with.

    The header extension area is envisioned to contain a sequence of self-identifying chunks. The flags field is not intended to tell readers what is in the extension area. Specific design of header extension contents is left for a later release.

    This design allows for both backwards-compatible header additions (add header extension chunks, or set low-order flag bits) and non-backwards-compatible changes (set high-order flag bits to signal such changes, and add supporting data to the extension area if needed).

    📦 Tuples

    Each tuple begins with a 16-bit integer count of the number of fields in the tuple. (Presently, all tuples in a table will have the same count, but that might not always be true.) Then, repeated for each field in the tuple, there is a 32-bit length word followed by that many bytes of field data. (The length word does not include itself, and can be zero.) As a special case, -1 indicates a NULL field value. No value bytes follow in the NULL case.

    There is no alignment padding or any other extra data between fields.

    Presently, all data values in a binary-format file are assumed to be in binary format (format code one). It is anticipated that a future extension might add a header field that allows per-column format codes to be specified.

    To determine the appropriate binary format for the actual tuple data you should consult the PostgreSQL source, in particular the *send and *recv functions for each column's data type (typically these functions are found in the src/backend/utils/adt/ directory of the source distribution).

    If OIDs are included in the file, the OID field immediately follows the field-count word. It is a normal field except that it's not included in the field-count. Note that oid system columns are not supported in current versions of PostgreSQL.

    🏁 File Trailer

    The file trailer consists of a 16-bit integer word containing -1. This is easily distinguished from a tuple's field-count word.

    A reader should report an error if a field-count word is neither -1 nor the expected number of columns. This provides an extra check against somehow getting out of sync with the data.

    💡 EXAMPLES

    The following example copies a table to the client using the vertical bar (|) as the field delimiter:

    COPY country TO STDOUT (DELIMITER '|');
    

    To copy data from a file into the country table:

    COPY country FROM '/usr1/proj/bray/sql/country_data';
    

    To copy into a file just the countries whose names start with 'A':

    COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';
    

    To copy into a compressed file, you can pipe the output through an external compression program:

    COPY country TO PROGRAM 'gzip > /usr1/proj/bray/sql/country_data.gz';
    

    Here is a sample of data suitable for copying into a table from STDIN:

    AF      AFGHANISTAN
    AL      ALBANIA
    DZ      ALGERIA
    ZM      ZAMBIA
    ZW      ZIMBABWE
    

    Note that the white space on each line is actually a tab character.

    The following is the same data, output in binary format. The data is shown after filtering through the Unix utility od -c. The table has three columns; the first has type char(2), the second has type text, and the third has type integer. All the rows have a null value in the third column.

    0000000   P   G   C   O   P   Y  \n 377  \r  \n  \0  \0  \0  \0  \0  \0
    0000020  \0  \0  \0  \0 003  \0  \0  \0 002   A   F  \0  \0  \0 013   A
    0000040   F   G   H   A   N   I   S   T   A   N 377 377 377 377  \0 003
    0000060  \0  \0  \0 002   A   L  \0  \0  \0 007   A   L   B   A   N   I
    0000100   A 377 377 377 377  \0 003  \0  \0  \0 002   D   Z  \0  \0  \0
    0000120 007   A   L   G   E   R   I   A 377 377 377 377  \0 003  \0  \0
    0000140  \0 002   Z   M  \0  \0  \0 006   Z   A   M   B   I   A 377 377
    0000160 377 377  \0 003  \0  \0  \0 002   Z   W  \0  \0  \0  \b   Z   I
    0000200   M   B   A   B   W   E 377 377 377 377 377 377
    

    🔄 COMPATIBILITY

    There is no COPY statement in the SQL standard.

    The following syntax was used before PostgreSQL version 9.0 and is still supported:

    COPY table_name [ ( column_name [, ...] ) ]
        FROM { 'filename' | STDIN }
        [ [ WITH ]
              [ BINARY ]
              [ DELIMITER [ AS ] 'delimiter_character' ]
              [ NULL [ AS ] 'null_string' ]
              [ CSV [ HEADER ]
                    [ QUOTE [ AS ] 'quote_character' ]
                    [ ESCAPE [ AS ] 'escape_character' ]
                    [ FORCE NOT NULL column_name [, ...] ] ] ]
    
    COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
        TO { 'filename' | STDOUT }
        [ [ WITH ]
              [ BINARY ]
              [ DELIMITER [ AS ] 'delimiter_character' ]
              [ NULL [ AS ] 'null_string' ]
              [ CSV [ HEADER ]
                    [ QUOTE [ AS ] 'quote_character' ]
                    [ ESCAPE [ AS ] 'escape_character' ]
                    [ FORCE QUOTE { column_name [, ...] | * } ] ] ]
    

    Note that in this syntax, BINARY and CSV are treated as independent keywords, not as arguments of a FORMAT option.

    The following syntax was used before PostgreSQL version 7.3 and is still supported:

    COPY [ BINARY ] table_name
        FROM { 'filename' | STDIN }
        [ [USING] DELIMITERS 'delimiter_character' ]
        [ WITH NULL AS 'null_string' ]
    
    COPY [ BINARY ] table_name
        TO { 'filename' | STDOUT }
        [ [USING] DELIMITERS 'delimiter_character' ]
        [ WITH NULL AS 'null_string' ]
    

    🔗 SEE ALSO

    Section 28.4.6

    ``` Wait, I need to check escaping more carefully. Let me scan: - `\N` is fine inside text - `\n` inside `` is fine - `\.` inside `` is fine - The od -c output uses `\n`, `\377`, `\r`, `\0`, `\003`, `\013`, `\007`, `\006`, `\b` - these are literal backslash characters in the displayed output (since od -c shows control chars as escapes). These should be preserved as-is in the `
    ` block.
    - `>` is already escaped in the original - good
    
    The h5 and h6 might be overkill. The rules say "Sub-sections within a section → 

    ". But the binary format has clear nesting: Binary Format > File Header > Signature. Let me use h3, h4, h5 for proper nesting. Actually, looking at the original HTML structure, the nested sections are quite deep. Let me check: - FILE FORMATS (h2) - Text Format (h3) - CSV Format (h3) - Binary Format (h3) - File Header (h4) - Signature (h5) - Flags field (h5) - Bit 16 (h6) - Header extension area length (h5) - Tuples (h4) - File Trailer (h4) That seems reasonable. Let me also check that I'm not putting `` in places they shouldn't be: - All HTML tags are allowed tags - All < in code are inside
    
    - The `>` is properly escaped
    
    Looking at the rules again about Quick Reference - I should put it as the second section (right after NAME). Got it.
    
    Let me also double-check the original `` tags - I should preserve their semantic meaning. In my output I'm using `` for COPY, which is equivalent. Actually the rule says "Preserve , ,  tags from the original" - so I should use  not . Let me adjust.
    
    Actually, looking again, the original has `NAME`, `SYNOPSIS`, etc. These are section markers I'm converting to 

    . The rule says preserve semantic meaning - converting these to

    preserves meaning. But within descriptions, there are no other tags in the original. The "Section 28.4.6" references are not bold in the original. So I should just use or for emphasis where needed in descriptions. Wait, looking at the original more carefully: - `NAME` → I'm converting to `

    📛 NAME

    ` - that's correct - `SYNOPSIS` → `

    📋 SYNOPSIS

    ` - correct - `
    COPY(7)` at top and bottom - I should preserve these? They're navigation/header/footer. Looking at the original, it's the page header and footer. Let me skip the top one (we're using a h2 title anyway) but maybe I should keep the references. Actually, looking at rule 17: "Preserve , , tags from the original — they carry semantic meaning." - I should preserve these. But the top `COPY(7)` is part of the page header which is essentially the page title. Since I'm creating a section structure with h2, I'll skip that as it's redundant with the NAME section. For the original document structure, the page begins with: `COPY(7) PostgreSQL 14.23 Documentation COPY(7)` This is the page header. I'll skip this entirely since I'm restructuring with h2 sections. For the footer: `PostgreSQL 14.23 2026 COPY(7)` I could add a footer, but it's not necessary. The rule is to preserve structure, but the page header/footer is metadata not content. Let me also reconsider the original `char(2)` - this is a reference link in the examples section. I should preserve that. Let me also check the Binary Format "Signature" - it says `PGCOPY\n\377\r\n\0` - this is using actual backslash-escaped characters. In my HTML output, this should be displayed literally as `PGCOPY\n\377\r\n\0` (where `\n` etc. are literal text showing escape sequences). Inside `` tags, this is fine. The od -c output also has these literal escape sequences shown by od -c when it encounters control characters. Let me also reconsider the Quick Reference table. I want it to be helpful. Let me refine it: Actually, looking back at the structure, the "Note" sections in the original use indentation to denote them as notes. I'll use `
    ` for these. Let me also double-check the binary format Signature h5 - actually I think h5 might be too deep. Let me simplify the heading hierarchy to h2-h3-h4 only. The original text uses indent levels, and while it goes deeper, I think h4 is enough for the deepest level. Let me reduce: - FILE FORMATS (h2) - Text Format (h3) - CSV Format (h3) - Binary Format (h3) - File Header (h4) - with content for Signature, Flags field, Bit 16, Header extension area length all under it - Tuples (h4) - File Trailer (h4) Then within File Header, I'll use or for Signature, Flags field, etc. instead of more headings. Actually, looking at the original, these are clearly subsection headers. But to keep it readable, I'll use h5 and h6 sparingly. Actually, let me keep it cleaner and use only h2-h3-h4. So: - FILE FORMATS (h2) - Text Format (h3) - CSV Format (h3) - Binary Format (h3) - File Header (h4) - **Signature** as bolded paragraph - **Flags field** as bolded paragraph - *Bit 16* as bolded paragraph - **Header extension area length** as bolded paragraph - Tuples (h4) - File Trailer (h4) Or I could use h5 for these. Hmm. Looking at HTML standards, h5 and h6 are valid. Let me use h5 for Signature, Flags field, etc. and h6 for Bit 16. That's actually cleaner. Actually you know what, let me keep the heading hierarchy simpler - h2, h3, h4 only. So: - FILE FORMATS (h2) - Text Format (h3) - CSV Format (h3) - Binary Format (h3) - File Header (h4) - Signature (h5) - Flags field (h5) - Bit 16 (h6) - subsubsection - Header extension area length (h5) - Tuples (h4) - File Trailer (h4) Hmm, but the rule says "Sub-sections within a section →

    ". It doesn't restrict further nesting. Let me use h5/h6 for the deepest levels as that's proper HTML. Actually, let me just use h5 throughout the binary format subsections to keep things consistent. OK let me just write the final version now. I'll keep heading levels h2-h4 for cleanliness, using h5 only when really needed for nesting (within the File Header section). Final check on character escaping: - All `` in code blocks are inside `
    ` - fine
    - `>` in `'gzip > /usr1/proj/...'` - already escaped - fine
    - `\n`, `\t`, etc. inside `
    ` and `` tags - these are literal backslash + letter sequences, no escaping needed
    - The od output contains lots of `\n`, `\r`, `\0`, etc. - literal text inside 
     - fine
    
    Let me write the final HTML. I'll aim for a clean, scannable output.
    
    

    📛 NAME

    COPY — copy data between a file and a table

    🚀 Quick Reference

    Use CaseCommandDescription
    📤 Export table to fileCOPY table TO '/path/file'Write all table data to a server-side file
    📥 Import file to tableCOPY table FROM '/path/file'Load data from file into table (appends)
    🖥️ Export to clientCOPY table TO STDOUTStream rows to client application
    ⌨️ Import from clientCOPY table FROM STDINRead rows from client input
    🔍 Export query resultsCOPY (SELECT ... FROM t WHERE ...) TO '/path'Export filtered/joined query output
    📊 CSV with headerCOPY table TO '/path' WITH (FORMAT csv, HEADER true)CSV export with column names row
    📦 Compressed exportCOPY table TO PROGRAM 'gzip > /path/file.gz'Pipe output through external program
    🚦 Conditional importCOPY table FROM '/path' WHERE col > 0Import only rows matching condition
    ❄️ Freeze on loadCOPY table FROM '/path' WITH (FREEZE true)Mark rows pre-frozen for speed
    🗜️ Binary formatCOPY table TO '/path' WITH (FORMAT binary)Faster binary dump (non-portable)

    📋 SYNOPSIS

    COPY table_name [ ( column_name [, ...] ) ]
        FROM { 'filename' | PROGRAM 'command' | STDIN }
        [ [ WITH ] ( option [, ...] ) ]
        [ WHERE condition ]
    
    COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
        TO { 'filename' | PROGRAM 'command' | STDOUT }
        [ [ WITH ] ( option [, ...] ) ]
    
    where option can be one of:
    
        FORMAT format_name
        FREEZE [ boolean ]
        DELIMITER 'delimiter_character'
        NULL 'null_string'
        HEADER [ boolean ]
        QUOTE 'quote_character'
        ESCAPE 'escape_character'
        FORCE_QUOTE { ( column_name [, ...] ) | * }
        FORCE_NOT_NULL ( column_name [, ...] )
        FORCE_NULL ( column_name [, ...] )
        ENCODING 'encoding_name'
    

    📖 DESCRIPTION

    COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already). COPY TO can also copy the results of a SELECT query.

    If a column list is specified, COPY TO copies only the data in the specified columns to the file. For COPY FROM, each field in the file is inserted, in order, into the specified column. Table columns not specified in the COPY FROM column list will receive their default values.

    COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible by the PostgreSQL user (the user ID the server runs as) and the name must be specified from the viewpoint of the server. When PROGRAM is specified, the server executes the given command and reads from the standard output of the program, or writes to the standard input of the program. The command must be specified from the viewpoint of the server, and be executable by the PostgreSQL user. When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server.

    📊 Each backend running COPY will report its progress in the pg_stat_progress_copy view. See Section 28.4.6 for details.

    ⚙️ PARAMETERS

    table_name

    The name (optionally schema-qualified) of an existing table.

    column_name

    An optional list of columns to be copied. If no column list is specified, all columns of the table except generated columns will be copied.

    query

    A SELECT, VALUES, INSERT, UPDATE, or DELETE command whose results are to be copied. Note that parentheses are required around the query.

    For INSERT, UPDATE and DELETE queries a RETURNING clause must be provided, and the target relation must not have a conditional rule, nor an ALSO rule, nor an INSTEAD rule that expands to multiple statements.

    filename

    The path name of the input or output file. An input file name can be an absolute or relative path, but an output file name must be an absolute path. Windows users might need to use an E'' string and double any backslashes used in the path name.

    PROGRAM

    A command to execute. In COPY FROM, the input is read from standard output of the command, and in COPY TO, the output is written to the standard input of the command.

    ⚠️ Note that the command is invoked by the shell, so if you need to pass any arguments to shell command that come from an untrusted source, you must be careful to strip or escape any special characters that might have a special meaning for the shell. For security reasons, it is best to use a fixed command string, or at least avoid passing any user input in it.

    STDIN

    Specifies that input comes from the client application.

    STDOUT

    Specifies that output goes to the client application.

    boolean

    Specifies whether the selected option should be turned on or off. You can write TRUE, ON, or 1 to enable the option, and FALSE, OFF, or 0 to disable it. The boolean value can also be omitted, in which case TRUE is assumed.

    FORMAT

    Selects the data format to be read or written: text, csv (Comma Separated Values), or binary. The default is text.

    FREEZE

    Requests copying the data with rows already frozen, just as they would be after running the VACUUM FREEZE command. This is intended as a performance option for initial data loading. Rows will be frozen only if the table being loaded has been created or truncated in the current subtransaction, there are no cursors open and there are no older snapshots held by this transaction. It is currently not possible to perform a COPY FREEZE on a partitioned table.

    Note that all other sessions will immediately be able to see the data once it has been successfully loaded. This violates the normal rules of MVCC visibility and users specifying should be aware of the potential problems this might cause.

    DELIMITER

    Specifies the character that separates columns within each row (line) of the file. The default is a tab character in text format, a comma in CSV format. This must be a single one-byte character. This option is not allowed when using binary format.

    NULL

    Specifies the string that represents a null value. The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format. You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings. This option is not allowed when using binary format.

    📝 Note: When using COPY FROM, any data item that matches this string will be stored as a null value, so you should make sure that you use the same string as you used with COPY TO.

    HEADER

    Specifies that the file contains a header line with the names of each column in the file. On output, the first line contains the column names from the table, and on input, the first line is ignored. This option is allowed only when using CSV format.

    QUOTE

    Specifies the quoting character to be used when a data value is quoted. The default is double-quote. This must be a single one-byte character. This option is allowed only when using CSV format.

    ESCAPE

    Specifies the character that should appear before a data character that matches the QUOTE value. The default is the same as the QUOTE value (so that the quoting character is doubled if it appears in the data). This must be a single one-byte character. This option is allowed only when using CSV format.

    FORCE_QUOTE

    Forces quoting to be used for all non-NULL values in each specified column. NULL output is never quoted. If * is specified, non-NULL values will be quoted in all columns. This option is allowed only in COPY TO, and only when using CSV format.

    FORCE_NOT_NULL

    Do not match the specified columns' values against the null string. In the default case where the null string is empty, this means that empty values will be read as zero-length strings rather than nulls, even when they are not quoted. This option is allowed only in COPY FROM, and only when using CSV format.

    FORCE_NULL

    Match the specified columns' values against the null string, even if it has been quoted, and if a match is found set the value to NULL. In the default case where the null string is empty, this converts a quoted empty string into NULL. This option is allowed only in COPY FROM, and only when using CSV format.

    ENCODING

    Specifies that the file is encoded in the encoding_name. If this option is omitted, the current client encoding is used. See the Notes below for more details.

    WHERE

    The optional WHERE clause has the general form

    WHERE condition
    

    where condition is any expression that evaluates to a result of type boolean. Any row that does not satisfy this condition will not be inserted to the table. A row satisfies the condition if it returns true when the actual row values are substituted for any variable references.

    Currently, subqueries are not allowed in WHERE expressions, and the evaluation does not see any changes made by the COPY itself (this matters when the expression contains calls to VOLATILE functions).

    📤 OUTPUTS

    On successful completion, a COPY command returns a command tag of the form

    COPY count
    

    The count is the number of rows copied.

    📝 Note: psql will print this command tag only if the command was not COPY ... TO STDOUT, or the equivalent psql meta-command \copy ... to stdout. This is to prevent confusing the command tag with the data that was just printed.

    📝 NOTES

    • 📋 COPY TO can be used only with plain tables, not views, and does not copy rows from child tables or child partitions. For example, COPY table TO copies the same rows as SELECT * FROM ONLY table. The syntax COPY (SELECT * FROM table) TO ... can be used to dump all of the rows in an inheritance hierarchy, partitioned table, or view.
    • 📥 COPY FROM can be used with plain, foreign, or partitioned tables or with views that have INSTEAD OF INSERT triggers.
    • 🔐 You must have select privilege on the table whose values are read by COPY TO, and insert privilege on the table into which values are inserted by COPY FROM. It is sufficient to have column privileges on the column(s) listed in the command.
    • 🛡️ If row-level security is enabled for the table, the relevant SELECT policies will apply to COPY table TO statements. Currently, COPY FROM is not supported for tables with row-level security. Use equivalent INSERT statements instead.
    • 📁 Files named in a COPY command are read or written directly by the server, not by the client application. Therefore, they must reside on or be accessible to the database server machine, not the client. They must be accessible to and readable or writable by the PostgreSQL user (the user ID the server runs as), not the client. Similarly, the command specified with PROGRAM is executed directly by the server, not by the client application, must be executable by the PostgreSQL user. COPY naming a file or command is only allowed to database superusers or users who are granted one of the roles pg_read_server_files, pg_write_server_files, or pg_execute_server_program, since it allows reading or writing any file or running a program that the server has privileges to access.
    • 🔄 Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.
    • 🛣️ It is recommended that the file name used in COPY always be specified as an absolute path. This is enforced by the server in the case of COPY TO, but for COPY FROM you do have the option of reading from a file specified by a relative path. The path will be interpreted relative to the working directory of the server process (normally the cluster's data directory), not the client's working directory.
    • 🔒 Executing a command with PROGRAM might be restricted by the operating system's access control mechanisms, such as SELinux.
    • ⚡ COPY FROM will invoke any triggers and check constraints on the destination table. However, it will not invoke rules.
    • 🆔 For identity columns, the COPY FROM command will always write the column values provided in the input data, like the INSERT option OVERRIDING SYSTEM VALUE.
    • 📅 COPY input and output is affected by DateStyle. To ensure portability to other PostgreSQL installations that might use non-default DateStyle settings, DateStyle should be set to ISO before using COPY TO. It is also a good idea to avoid dumping data with IntervalStyle set to sql_standard, because negative interval values might be misinterpreted by a server that has a different setting for IntervalStyle.
    • 🌐 Input data is interpreted according to ENCODING option or the current client encoding, and output data is encoded in ENCODING or the current client encoding, even if the data does not pass through the client but is read from or written to a file directly by the server.
    • ⛔ COPY stops operation at the first error. This should not lead to problems in the event of a COPY TO, but the target table will already have received earlier rows in a COPY FROM. These rows will not be visible or accessible, but they still occupy disk space. This might amount to a considerable amount of wasted disk space if the failure happened well into a large copy operation. You might wish to invoke VACUUM to recover the wasted space.
    • 🔀 FORCE_NULL and FORCE_NOT_NULL can be used simultaneously on the same column. This results in converting quoted null strings to null values and unquoted null strings to empty strings.

    📂 FILE FORMATS

    📄 Text Format

    When the text format is used, the data read or written is a text file with one line per table row. Columns in a row are separated by the delimiter character. The column values themselves are strings generated by the output function, or acceptable to the input function, of each attribute's data type. The specified null string is used in place of columns that are null. COPY FROM will raise an error if any line of the input file contains more or fewer columns than are expected.

    End of data can be represented by a single line containing just backslash-period (\.). An end-of-data marker is not necessary when reading from a file, since the end of file serves perfectly well; it is needed only when copying data to or from client applications using pre-3.0 client protocol.

    Backslash characters (\) can be used in the COPY data to quote data characters that might otherwise be taken as row or column delimiters. In particular, the following characters must be preceded by a backslash if they appear as part of a column value: backslash itself, newline, carriage return, and the current delimiter character.

    The specified null string is sent by COPY TO without adding any backslashes; conversely, COPY FROM matches the input against the null string before removing backslashes. Therefore, a null string such as \N cannot be confused with the actual data value \N (which would be represented as \\N).

    The following special backslash sequences are recognized by COPY FROM:

    SequenceRepresents
    \bBackspace (ASCII 8)
    \fForm feed (ASCII 12)
    \nNewline (ASCII 10)
    \rCarriage return (ASCII 13)
    \tTab (ASCII 9)
    \vVertical tab (ASCII 11)
    \digitsBackslash followed by one to three octal digits specifies the byte with that numeric code
    \xdigitsBackslash x followed by one or two hex digits specifies the byte with that numeric code

    Presently, COPY TO will never emit an octal or hex-digits backslash sequence, but it does use the other sequences listed above for those control characters.

    Any other backslashed character that is not mentioned in the above table will be taken to represent itself. However, beware of adding backslashes unnecessarily, since that might accidentally produce a string matching the end-of-data marker (\.) or the null string (\N by default). These strings will be recognized before any other backslash processing is done.

    It is strongly recommended that applications generating COPY data convert data newlines and carriage returns to the \n and \r sequences respectively. At present it is possible to represent a data carriage return by a backslash and carriage return, and to represent a data newline by a backslash and newline. However, these representations might not be accepted in future releases. They are also highly vulnerable to corruption if the COPY file is transferred across different machines (for example, from Unix to Windows or vice versa).

    All backslash sequences are interpreted after encoding conversion. The bytes specified with the octal and hex-digit backslash sequences must form valid characters in the database encoding.

    COPY TO will terminate each row with a Unix-style newline (\n). Servers running on Microsoft Windows instead output carriage return/newline (\r\n), but only for COPY to a server file; for consistency across platforms, COPY TO STDOUT always sends \n regardless of server platform. COPY FROM can handle lines ending with newlines, carriage returns, or carriage return/newlines. To reduce the risk of error due to un-backslashed newlines or carriage returns that were meant as data, COPY FROM will complain if the line endings in the input are not all alike.

    📊 CSV Format

    This format option is used for importing and exporting the Comma Separated Value (CSV) file format used by many other programs, such as spreadsheets. Instead of the escaping rules used by PostgreSQL's standard text format, it produces and recognizes the common CSV escaping mechanism.

    The values in each record are separated by the DELIMITER character. If the value contains the delimiter character, the QUOTE character, the NULL string, a carriage return, or line feed character, then the whole value is prefixed and suffixed by the QUOTE character, and any occurrence within the value of a QUOTE character or the ESCAPE character is preceded by the escape character. You can also use FORCE_QUOTE to force quotes when outputting non-NULL values in specific columns.

    The CSV format has no standard way to distinguish a NULL value from an empty string. PostgreSQL's COPY handles this by quoting. A NULL is output as the NULL parameter string and is not quoted, while a non-NULL value matching the NULL parameter string is quoted. For example, with the default settings, a NULL is written as an unquoted empty string, while an empty string data value is written with double quotes (""). Reading values follows similar rules. You can use FORCE_NOT_NULL to prevent NULL input comparisons for specific columns. You can also use FORCE_NULL to convert quoted null string data values to NULL.

    Because backslash is not a special character in the CSV format, \., the end-of-data marker, could also appear as a data value. To avoid any misinterpretation, a \. data value appearing as a lone entry on a line is automatically quoted on output, and on input, if quoted, is not interpreted as the end-of-data marker. If you are loading a file created by another application that has a single unquoted column and might have a value of \., you might need to quote that value in the input file.

    📝 Note: In CSV format, all characters are significant. A quoted value surrounded by white space, or any characters other than DELIMITER, will include those characters. This can cause errors if you import data from a system that pads CSV lines with white space out to some fixed width. If such a situation arises you might need to preprocess the CSV file to remove the trailing white space, before importing the data into PostgreSQL.

    📝 Note: CSV format will both recognize and produce CSV files with quoted values containing embedded carriage returns and line feeds. Thus the files are not strictly one line per table row like text-format files.

    📝 Note: Many programs produce strange and occasionally perverse CSV files, so the file format is more a convention than a standard. Thus you might encounter some files that cannot be imported using this mechanism, and COPY might produce files that other programs cannot process.

    🔢 Binary Format

    The binary format option causes all data to be stored/read as binary format rather than as text. It is somewhat faster than the text and CSV formats, but a binary-format file is less portable across machine architectures and PostgreSQL versions. Also, the binary format is very data type specific; for example it will not work to output binary data from a smallint column and read it into an integer column, even though that would work fine in text format.

    The binary file format consists of a file header, zero or more tuples containing the row data, and a file trailer. Headers and data are in network byte order.

    📝 Note: PostgreSQL releases before 7.4 used a different binary file format.

    📋 File Header

    The file header consists of 15 bytes of fixed fields, followed by a variable-length header extension area. The fixed fields are:

    🖋️ Signature

    11-byte sequence PGCOPY\n\377\r\n\0 — note that the zero byte is a required part of the signature. (The signature is designed to allow easy identification of files that have been munged by a non-8-bit-clean transfer. This signature will be changed by end-of-line-translation filters, dropped zero bytes, dropped high bits, or parity changes.)

    🚩 Flags field

    32-bit integer bit mask to denote important aspects of the file format. Bits are numbered from 0 (LSB) to 31 (MSB). Note that this field is stored in network byte order (most significant byte first), as are all the integer fields used in the file format. Bits 16-31 are reserved to denote critical file format issues; a reader should abort if it finds an unexpected bit set in this range. Bits 0-15 are reserved to signal backwards-compatible format issues; a reader should simply ignore any unexpected bits set in this range. Currently only one flag bit is defined, and the rest must be zero:

    🔢 Bit 16

    If 1, OIDs are included in the data; if 0, not. Oid system columns are not supported in PostgreSQL anymore, but the format still contains the indicator.

    📏 Header extension area length

    32-bit integer, length in bytes of remainder of header, not including self. Currently, this is zero, and the first tuple follows immediately. Future changes to the format might allow additional data to be present in the header. A reader should silently skip over any header extension data it does not know what to do with.

    The header extension area is envisioned to contain a sequence of self-identifying chunks. The flags field is not intended to tell readers what is in the extension area. Specific design of header extension contents is left for a later release.

    This design allows for both backwards-compatible header additions (add header extension chunks, or set low-order flag bits) and non-backwards-compatible changes (set high-order flag bits to signal such changes, and add supporting data to the extension area if needed).

    📦 Tuples

    Each tuple begins with a 16-bit integer count of the number of fields in the tuple. (Presently, all tuples in a table will have the same count, but that might not always be true.) Then, repeated for each field in the tuple, there is a 32-bit length word followed by that many bytes of field data. (The length word does not include itself, and can be zero.) As a special case, -1 indicates a NULL field value. No value bytes follow in the NULL case.

    There is no alignment padding or any other extra data between fields.

    Presently, all data values in a binary-format file are assumed to be in binary format (format code one). It is anticipated that a future extension might add a header field that allows per-column format codes to be specified.

    To determine the appropriate binary format for the actual tuple data you should consult the PostgreSQL source, in particular the *send and *recv functions for each column's data type (typically these functions are found in the src/backend/utils/adt/ directory of the source distribution).

    If OIDs are included in the file, the OID field immediately follows the field-count word. It is a normal field except that it's not included in the field-count. Note that oid system columns are not supported in current versions of PostgreSQL.

    🏁 File Trailer

    The file trailer consists of a 16-bit integer word containing -1. This is easily distinguished from a tuple's field-count word.

    A reader should report an error if a field-count word is neither -1 nor the expected number of columns. This provides an extra check against somehow getting out of sync with the data.

    💡 EXAMPLES

    The following example copies a table to the client using the vertical bar (|) as the field delimiter:

    COPY country TO STDOUT (DELIMITER '|');
    

    To copy data from a file into the country table:

    COPY country FROM '/usr1/proj/bray/sql/country_data';
    

    To copy into a file just the countries whose names start with 'A':

    COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';
    

    To copy into a compressed file, you can pipe the output through an external compression program:

    COPY country TO PROGRAM 'gzip > /usr1/proj/bray/sql/country_data.gz';
    

    Here is a sample of data suitable for copying into a table from STDIN:

    AF      AFGHANISTAN
    AL      ALBANIA
    DZ      ALGERIA
    ZM      ZAMBIA
    ZW      ZIMBABWE
    

    Note that the white space on each line is actually a tab character.

    The following is the same data, output in binary format. The data is shown after filtering through the Unix utility od -c. The table has three columns; the first has type char(2), the second has type text, and the third has type integer. All the rows have a null value in the third column.

    0000000   P   G   C   O   P   Y  \n 377  \r  \n  \0  \0  \0  \0  \0  \0
    0000020  \0  \0  \0  \0 003  \0  \0  \0 002   A   F  \0  \0  \0 013   A
    0000040   F   G   H   A   N   I   S   T   A   N 377 377 377 377  \0 003
    0000060  \0  \0  \0 002   A   L  \0  \0  \0 007   A   L   B   A   N   I
    0000100   A 377 377 377 377  \0 003  \0  \0  \0 002   D   Z  \0  \0  \0
    0000120 007   A   L   G   E   R   I   A 377 377 377 377  \0 003  \0  \0
    0000140  \0 002   Z   M  \0  \0  \0 006   Z   A   M   B   I   A 377 377
    0000160 377 377  \0 003  \0  \0  \0 002   Z   W  \0  \0  \0  \b   Z   I
    0000200   M   B   A   B   W   E 377 377 377 377 377 377
    

    🔄 COMPATIBILITY

    There is no COPY statement in the SQL standard.

    The following syntax was used before PostgreSQL version 9.0 and is still supported:

    COPY table_name [ ( column_name [, ...] ) ]
        FROM { 'filename' | STDIN }
        [ [ WITH ]
              [ BINARY ]
              [ DELIMITER [ AS ] 'delimiter_character' ]
              [ NULL [ AS ] 'null_string' ]
              [ CSV [ HEADER ]
                    [ QUOTE [ AS ] 'quote_character' ]
                    [ ESCAPE [ AS ] 'escape_character' ]
                    [ FORCE NOT NULL column_name [, ...] ] ] ]
    
    COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
        TO { 'filename' | STDOUT }
        [ [ WITH ]
              [ BINARY ]
              [ DELIMITER [ AS ] 'delimiter_character' ]
              [ NULL [ AS ] 'null_string' ]
              [ CSV [ HEADER ]
                    [ QUOTE [ AS ] 'quote_character' ]
                    [ ESCAPE [ AS ] 'escape_character' ]
                    [ FORCE QUOTE { column_name [, ...] | * } ] ] ]
    

    Note that in this syntax, BINARY and CSV are treated as independent keywords, not as arguments of a FORMAT option.

    The following syntax was used before PostgreSQL version 7.3 and is still supported:

    COPY [ BINARY ] table_name
        FROM { 'filename' | STDIN }
        [ [USING] DELIMITERS 'delimiter_character' ]
        [ WITH NULL AS 'null_string' ]
    
    COPY [ BINARY ] table_name
        TO { 'filename' | STDOUT }
        [ [USING] DELIMITERS 'delimiter_character' ]
        [ WITH NULL AS 'null_string' ]
    

    🔗 SEE ALSO

    Section 28.4.6

    Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
    2026-08-14 21:09 @2600:1f28:365:80b0:4d23:66fa:c2bb:7bae
    CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
    Valid XHTML 1.0 Transitional!Valid CSS!