[英]Import data from Excel to PostgreSQL
I have seen questions on stackoverflow similar/same as the one I am asking now, however I couldn't manage to solve it in my situation. 我已经看到了关于stackoverflow的问题,与我现在所问的问题类似/相同,但是在我的情况下,我无法解决它。
Here is the thing: I have an excel spreadsheet(.xlsx) whom i converted in comma seperated value(.CSV) as it is said in some answers: 事情是这样的:我有一个excel电子表格(.xlsx),我将其转换为逗号分隔值(.CSV),如在某些答案中所述:
My excel file looks something like this: 我的excel文件看起来像这样:
--------------------------------------------------
name | surname | voteNo | VoteA | VoteB | VoteC
--------------------------------------------------
john | smith | 1001 | 30 | 154 | 25
--------------------------------------------------
anothe| person | 1002 | 430 | 34 | 234
--------------------------------------------------
other | one | 1003 | 35 | 154 | 24
--------------------------------------------------
john | smith | 1004 | 123 | 234 | 53
--------------------------------------------------
john | smith | 1005 | 23 | 233 | 234
--------------------------------------------------
In PostgreSQL I created a table with name allfields
and created 6 columns 1st and 2nd one as a character[] and last 4 ones as integers with the same name as shown in the excel table (name, surname, voteno, votea, voteb, votec)
在PostgreSQL中,我创建了一个表,该表的名称为
allfields
并创建了6列第1列和第2列作为character [],最后4列作为整数,其名称与excel表中显示的名称相同(name, surname, voteno, votea, voteb, votec)
Now I'm doing this: 现在我正在这样做:
copy allfields from 'C:\Filepath\filename.csv';
But I'm getting this error: 但我收到此错误:
could not open file "C:\\Filepath\\filename.csv" for reading: Permission denied SQL state: 42501
allfields
table in PostgreSQL? allfields
表中创建这些列吗? varchar
as the type of those two columns instead. varchar
用作这两列的类型。 SQL state: 42501 in PostgreSQL means you don't have permission to perform such operation in the intended schema. SQL状态: PostgreSQL中的42501表示您无权在预期的模式中执行此类操作。 This error code list shows that.
此错误代码列表显示了这一点。
Check that you're pointing to the correct schema and your user has enough privileges. 检查您是否指向正确的架构,并且您的用户具有足够的特权。
Documentation also states that you need select privileges on origin table and insert privileges on the destination table. 文档还指出 ,您需要在原始表上具有选择特权,并在目标表上具有插入特权。
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.
您必须对通过COPY TO读取其值的表具有选择特权,并且对于通过COPY FROM将值插入其中的表具有插入特权。 It is sufficient to have column privileges on the column(s) listed in the command.
在命令中列出的列上具有列特权就足够了。
1) Should I create those columns in allfields table in PostgreSQL?
1)我应该在PostgreSQL的allfields表中创建那些列吗?
Use text
for the character fields. 在字符字段中使用
text
。 Not an array in any case, as @yieldsfalsehood pointed out correctly . 在任何情况下都不是数组,正如@yieldsfalsehood正确指出的那样 。
2) Do I have to modify anything else in Excel file?
2)我必须修改Excel文件中的其他内容吗?
No. 没有。
3) And why I get this 'permission denied' error?
3)为什么我会收到此“权限被拒绝”错误?
The file needs be accessible to your system user postgres
(or what ever user you are running the postgres server with). 您的系统用户
postgres
(或与之运行postgres服务器的任何用户)都需要访问该文件。 Per documentation: 每个文档:
COPY
with a file name instructs the PostgreSQL server to directly read from or write to a file.带有文件名的
COPY
指示PostgreSQL服务器直接从文件读取或写入文件。 The file must be accessible to the server and the name must be specified from the viewpoint of the server.服务器必须可以访问该文件,并且必须从服务器的角度指定名称。
The privileges of the database user are not the cause of the problem. 数据库用户的特权不是问题的原因。 However (quoting the same page):
但是(引用同一页):
COPY
naming a file or command is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access.仅允许数据库超级用户使用
COPY
命名文件或命令,因为它允许读取或写入服务器有权访问的任何文件。
关于权限问题,如果使用psql发出COPY
命令,请尝试使用\\copy
。
Ok the Problem was that i need to change the path
of the Excel file
. 好的,问题是我需要更改
Excel file
的path
。 I inserted it in the public account where all users can access it . 我将其插入所有用户都可以访问的公共帐户中。
If you face the same problem move your excel file
to ex C:\\\\User\\Public
folder (this folder is a public folder without any restrictions), otherwise you have to deal with Windows permission issues
. 如果您遇到相同的问题,请将
excel file
移到C:\\\\User\\Public
文件夹(此文件夹是没有任何限制的公用文件夹),否则您必须处理Windows permission issues
。
For those who do not wish to move the files they wish to read to a different location(public) for some reason. 对于那些由于某些原因不希望将文件移动到其他位置(公共)的用户。 Here is a clear solution.
这是一个明确的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.