⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copy.7

📁 PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开发团队说,该版本将加速更多企业向该数据库移植.核心开发成员之一Bruce Momjian表示,在新版PostgreSQL
💻 7
📖 第 1 页 / 共 2 页
字号:
unnecessarily, since that might accidentally produce a string matching theend-of-data marker (\\.) or the null string (\\N bydefault). These strings will be recognized before any other backslashprocessing is done..PPIt is strongly recommended that applications generating \fBCOPY\fR data convertdata newlines and carriage returns to the \\n and\\r sequences respectively. At present it ispossible to represent a data carriage return by a backslash and carriagereturn, 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 \fBCOPY\fR file istransferred across different machines (for example, from Unix to Windowsor vice versa)..PP\fBCOPY TO\fR will terminate each row with a Unix-style newline (``\\n''). Servers running on Microsoft Windows insteadoutput carriage return/newline (``\\r\\n''), but only for\fBCOPY\fR to a server file; for consistency across platforms,\fBCOPY TO STDOUT\fR always sends ``\\n''regardless of server platform.\fBCOPY FROM\fR can handle lines ending with newlines,carriage returns, or carriage return/newlines. To reduce the risk oferror due to un-backslashed newlines or carriage returns that weremeant as data, \fBCOPY FROM\fR will complain if the lineendings in the input are not all alike..SS "CSV FORMAT".PPThis format is used for importing and exporting the CommaSeparated Value (CSV) file format used by many otherprograms, such as spreadsheets. Instead of the escaping used byPostgreSQL's standard text mode, itproduces and recognizes the common CSV escaping mechanism..PPThe values in each record are separated by the DELIMITERcharacter. If the value contains the delimiter character, theQUOTE character, the NULL string, a carriagereturn, or line feed character, then the whole value is prefixed andsuffixed by the QUOTE character, and any occurrencewithin the value of a QUOTE character or theESCAPE character is preceded by the escape character.You can also use FORCE QUOTE to force quotes when outputtingnon-NULL values in specific columns..PPThe CSV format has no standard way to distinguish aNULL value from an empty string.PostgreSQL's \fBCOPY\fR handles this byquoting. A NULL is output as the NULLstring and is not quoted, while a data value matching theNULL string is quoted. Therefore, using the defaultsettings, a NULL is written as an unquoted emptystring, while an empty string is written with double quotes(""). Reading values follows similar rules. You canuse FORCE NOT NULL to prevent NULL inputcomparisons for specific columns..PPBecause backslash is not a special character in the CSVformat, \\., the end-of-data marker, could also appearas 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 anotherapplication that has a single unquoted column and might have a value of \\., you might need to quote that value in the input file..sp.RS.B "Note:"In CSV mode, 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. .RE.sp.sp.RS.B "Note:"CSV mode will both recognize and produce CSV files with quotedvalues containing embedded carriage returns and line feeds. Thusthe files are not strictly one line per table row like text-modefiles..RE.sp.sp.RS.B "Note:"Many programs produce strange and occasionally perverse CSV files,so the file format is more a convention than a standard. Thus youmight encounter some files that cannot be imported using thismechanism, and \fBCOPY\fR might produce files that otherprograms cannot process..RE.sp.SS "BINARY FORMAT".PPThe file format used for \fBCOPY BINARY\fR changed inPostgreSQL 7.4. The new format consistsof a file header, zero or more tuples containing the row data, anda file trailer. Headers and data are now in network byte order..SS "FILE HEADER".PPThe file header consists of 15 bytes of fixed fields, followedby a variable-length header extension area. The fixed fields are:.TP\fBSignature\fR11-byte sequence PGCOPY\\n\\377\\r\\n\\0 \(em note that the zero byteis a required part of the signature. (The signature is designed to alloweasy identification of files that have been munged by a non-8-bit-cleantransfer. This signature will be changed by end-of-line-translationfilters, dropped zero bytes, dropped high bits, or parity changes.).TP\fBFlags field\fR32-bit integer bit mask to denote important aspects of the file format. Bitsare numbered from 0 (LSB) to 31 (MSB). Note thatthis field is stored in network byte order (most significant byte first),as are all the integer fields used in the file format. Bits16-31 are reserved to denote critical file format issues; a readershould abort if it finds an unexpected bit set in this range. Bits 0-15are reserved to signal backwards-compatible format issues; a readershould simply ignore any unexpected bits set in this range. Currentlyonly one flag bit is defined, and the rest must be zero:.RS.TP\fBBit 16\fRif 1, OIDs are included in the data; if 0, not.RE.PP.TP\fBHeader extension area length\fR32-bit integer, length in bytes of remainder of header, not including self.Currently, this is zero, and the first tuple followsimmediately. Future changes to the format might allow additional datato be present in the header. A reader should silently skip over any headerextension data it does not know what to do with..PP.PPThe header extension area is envisioned to contain a sequence ofself-identifying chunks. The flags field is not intended to tell readerswhat is in the extension area. Specific design of header extension contentsis left for a later release..PPThis design allows for both backwards-compatible header additions (addheader extension chunks, or set low-order flag bits) andnon-backwards-compatible changes (set high-order flag bits to signal suchchanges, and add supporting data to the extension area if needed)..SS "TUPLES".PPEach tuple begins with a 16-bit integer count of the number of fields in thetuple. (Presently, all tuples in a table will have the same count, but thatmight not always be true.) Then, repeated for each field in the tuple, thereis a 32-bit length word followed by that many bytes of field data. (Thelength 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..PPThere is no alignment padding or any other extra data between fields..PPPresently, all data values in a \fBCOPY BINARY\fR file areassumed to be in binary format (format code one). It is anticipated that afuture extension may add a header field that allows per-column format codesto be specified..PPTo determine the appropriate binary format for the actual tuple data youshould consult the PostgreSQL source, inparticular the \fB*send\fR and \fB*recv\fR functions foreach column's data type (typically these functions are found in the\fIsrc/backend/utils/adt/\fR directory of the sourcedistribution)..PPIf OIDs are included in the file, the OID field immediately follows thefield-count word. It is a normal field except that it's not includedin the field-count. In particular it has a length word \(em this will allowhandling of 4-byte vs. 8-byte OIDs without too much pain, and will allowOIDs to be shown as null if that ever proves desirable..SS "FILE TRAILER".PPThe file trailer consists of a 16-bit integer word containing -1. Thisis easily distinguished from a tuple's field-count word..PPA reader should report an error if a field-count word is neither -1nor the expected number of columns. This provides an extracheck against somehow getting out of sync with the data..SH "EXAMPLES".PPThe following example copies a table to the clientusing the vertical bar (|) as the field delimiter:.sp.nfCOPY country TO STDOUT WITH DELIMITER '|';.sp.fi.PPTo copy data from a file into the country table:.sp.nfCOPY country FROM '/usr1/proj/bray/sql/country_data';.sp.fi.PPTo copy into a file just the countries whose names start with 'A':.sp.nfCOPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';.sp.fi.PPHere is a sample of data suitable for copying into a table fromSTDIN:.sp.nfAF      AFGHANISTANAL      ALBANIADZ      ALGERIAZM      ZAMBIAZW      ZIMBABWE.sp.fiNote that the white space on each line is actually a tab character..PPThe following is the same data, output in binary format.The data is shown after filtering through theUnix utility \fBod -c\fR. The table has three columns;the first has type \fBchar(2)\fR, the second has type \fBtext\fR,and the third has type \fBinteger\fR. All the rows have a null valuein the third column..sp.nf0000000   P   G   C   O   P   Y  \\n 377  \\r  \\n  \\0  \\0  \\0  \\0  \\0  \\00000020  \\0  \\0  \\0  \\0 003  \\0  \\0  \\0 002   A   F  \\0  \\0  \\0 013   A0000040   F   G   H   A   N   I   S   T   A   N 377 377 377 377  \\0 0030000060  \\0  \\0  \\0 002   A   L  \\0  \\0  \\0 007   A   L   B   A   N   I0000100   A 377 377 377 377  \\0 003  \\0  \\0  \\0 002   D   Z  \\0  \\0  \\00000120 007   A   L   G   E   R   I   A 377 377 377 377  \\0 003  \\0  \\00000140  \\0 002   Z   M  \\0  \\0  \\0 006   Z   A   M   B   I   A 377 3770000160 377 377  \\0 003  \\0  \\0  \\0 002   Z   W  \\0  \\0  \\0  \\b   Z   I0000200   M   B   A   B   W   E 377 377 377 377 377 377.sp.fi.SH "COMPATIBILITY".PPThere is no \fBCOPY\fR statement in the SQL standard..PPThe following syntax was used before PostgreSQLversion 7.3 and is still supported:.sp.nfCOPY [ BINARY ] \fItablename\fR [ WITH OIDS ]    FROM { '\fIfilename\fR' | STDIN }    [ [USING] DELIMITERS '\fIdelimiter\fR' ]    [ WITH NULL AS '\fInull string\fR' ]COPY [ BINARY ] \fItablename\fR [ WITH OIDS ]    TO { '\fIfilename\fR' | STDOUT }    [ [USING] DELIMITERS '\fIdelimiter\fR' ]    [ WITH NULL AS '\fInull string\fR' ].sp.fi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -