简体   繁体   English

将数据从Excel导入PostgreSQL

[英]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 

My questions are: 我的问题是:

  1. Should I create those columns in allfields table in PostgreSQL? 我应该在PostgreSQL的allfields表中创建这些列吗?
  2. Do I have to modify anything else in Excel file? 我是否需要修改Excel文件中的其他内容?
  3. And why I get this 'permission denied' error? 为什么会出现此“权限被拒绝”错误?
  1. Based on your file, neither of the first two columns needs to be an array type (character[]) - unlike C-strings, the "character" type in postgres is a string already. 根据您的文件,前两列都不需要为数组类型(character [])-与C字符串不同,postgres中的“字符”类型已经字符串。 You might want to make things easier and use varchar as the type of those two columns instead. 您可能希望使事情变得更简单,而将varchar用作这两列的类型。
  2. I don't think you do. 我不认为你这样做。
  3. Check that you don't still have that file open and locked in excel - if you did a "save as" to convert from xlsx to csv from within excel then you'll likely need to close out the file in excel. 检查您是否仍未在excel中打开并锁定该文件-如果您执行了“另存为”操作以从excel中将xlsx转换为csv,则可能需要在excel中关闭该文件。

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. Yes I think you can. 是的,我想你可以。 For COPY command, there is optional HEADER clause. 对于COPY命令,有可选的HEADER子句。 Check http://www.postgresql.org/docs/9.2/static/sql-copy.html 检查http://www.postgresql.org/docs/9.2/static/sql-copy.html
  2. I don't think so. 我不这么认为。 With my #1 and #3, it should works. 对于我的#1和#3,它应该可以工作。
  3. You need superuser permission for that. 为此,您需要超级用户权限。

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 filepath 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. 这是一个明确的解决方案。

  1. Right click the folder holding the file and select properties . 右键单击保存文件的文件夹,然后选择属性
  2. Select the Security tab under properties. 选择属性下的安全选项卡。
  3. Select Edit 选择编辑
  4. Select Add 选择添加
  5. Under the field Enter the object Names to select , Type in Everyone 在“ 输入要选择的对象名称 ”字段下,键入“ 所有人”
  6. Click OK to all the dialog boxes or Apply if it is activated 单击“ 确定”进入所有对话框,或单击“激活”。
  7. Try reading the file again. 尝试再次读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM