PostgreSQL 中的 COPY 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10079682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
COPY function in PostgreSQL
提问by Jeiman
I would like to use the COPY function in PostgreSQL to import a CSV file into a PostgreSQL database.
我想使用 PostgreSQL 中的 COPY 函数将 CSV 文件导入到 PostgreSQL 数据库中。
Where it says the filename in the documentation, does the CSV file have to be stored in a specific location or can it be stored in any location.
在文档中说文件名的地方,CSV 文件必须存储在特定位置还是可以存储在任何位置。
For example, copy data_table from '/tmp/outputdata.csv' WITH DELIMITER AS ',' CSV QUOTE AS '"';
. Where it says tmp
, does that mean the tmp folder in the C: drive. Can it be change to another folder name?
例如,copy data_table from '/tmp/outputdata.csv' WITH DELIMITER AS ',' CSV QUOTE AS '"';
。它说的地方是否tmp
意味着 C: 驱动器中的 tmp 文件夹。是否可以更改为另一个文件夹名称?
回答by Erwin Brandstetter
It looks like you are confused by Linux vs. Windows file-path notation. What you have there is a Linux path anchored to root. Windows uses drive letters, which you can specify just as well when you are running on Windows.
看起来您对 Linux 与 Windows 文件路径表示法感到困惑。你所拥有的是一个锚定到 root 的 Linux 路径。Windows 使用驱动器号,当您在 Windows 上运行时也可以指定驱动器号。
If you use Windows notation, take care that you have to escape backslashesif you are not using standard_conforming_strings = on
- which is the default in Postgres 9.1 or later, but not in older versions. Like:
如果您使用 Windows 表示法,请注意在不使用时必须转义反斜杠standard_conforming_strings = on
- 这是 Postgres 9.1 或更高版本中的默认值,但在旧版本中则不然。喜欢:
COPY data_table from E'C:\tmp\outputdata.csv' WITH ...
With standard_conforming_strings = on
you can simply write:
有了standard_conforming_strings = on
可以简单的写:
COPY data_table from 'C:\tmp\outputdata.csv' WITH ...
Note that a PostgreSQL Windows server also understands default path notation with slashes instead of backslashes.
请注意,PostgreSQL Windows 服务器也理解带有斜杠而不是反斜杠的默认路径表示法。
For SQL COPY FROM / TO
you can use any path that the owner of server process (postgres
by default) has permission to read / write.
对于 SQL,COPY FROM / TO
您可以使用服务器进程的所有者(postgres
默认情况下)有权读/写的任何路径。
For the \copy
meta command of the psql client the permissions of current local user apply.
对于\copy
psql 客户端的meta 命令,当前本地用户的权限适用。
回答by aleroot
Yes, of course you can specify whatever location where you have read access. There's no problem changing the path of the file.
是的,当然您可以指定您拥有读取权限的任何位置。更改文件路径没有问题。
Keep attention only on the fact that on windows you have to escape the backslash in this way :
只注意在 Windows 上你必须以这种方式逃避反斜杠的事实:
copy data_table from 'c:\Temp\outputdata.csv' WITH DELIMITER AS ',' CSV QUOTE AS '"';