如何在Postgres-Heroku中插入CSV中的批量数据?

xhv8bpkk  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(143)

我已经使用dbeaver将我的托管postgres-db从heroku连接到我的本地机器,连接成功,当我尝试一次插入单个或多个数据(多个但不是批量)时,它确实如此,但当我尝试从CSV文件批量插入数据时,它失败了,并给出以下错误:

SQL Error [42501]: ERROR: must be superuser or have privileges of the pg_read_server_files role to COPY from a file
  Hint: Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone

字符串
数据看起来像这样:

,title,ingredients,directions,link,source,NER
0,No-Bake Nut Cookies,"[""1 c. firmly packed brown sugar"", ""1/2 c. evaporated milk"", ""1/2 tsp. vanilla"", ""1/2 c. broken nuts (pecans)"", ""2 Tbsp. butter or margarine"", ""3 1/2 c. bite size shredded rice biscuits""]","[""In a heavy 2-quart saucepan, mix brown sugar, nuts, evaporated milk and butter or margarine."", ""Stir over medium heat until mixture bubbles all over top."", ""Boil and stir 5 minutes more. Take off heat."", ""Stir in vanilla and cereal; mix well."", ""Using 2 teaspoons, drop and shape into 30 clusters on wax paper."", ""Let stand until firm, about 30 minutes.""]",www.cookbooks.com/Recipe-Details.aspx?id=44874,Gathered,"[""brown sugar"", ""milk"", ""vanilla"", ""nuts"", ""butter"", ""bite size shredded rice biscuits""]"
1,Jewell Ball'S Chicken,"[""1 small jar chipped beef, cut up"", ""4 boned chicken breasts"", ""1 can cream of mushroom soup"", ""1 carton sour cream""]","[""Place chipped beef on bottom of baking dish."", ""Place chicken on top of beef."", ""Mix soup and cream together; pour over chicken. Bake, uncovered, at 275\u00b0 for 3 hours.""]",www.cookbooks.com/Recipe-Details.aspx?id=699419,Gathered,"[""beef"", ""chicken breasts"", ""cream of mushroom soup"", ""sour cream""]"
2,Creamy Corn,"[""2 (16 oz.) pkg. frozen corn"", ""1 (8 oz.) pkg. cream cheese, cubed"", ""1/3 c. butter, cubed"", ""1/2 tsp. garlic powder"", ""1/2 tsp. salt"", ""1/4 tsp. pepper""]","[""In a slow cooker, combine all ingredients. Cover and cook on low for 4 hours or until heated through and cheese is melted. Stir well before serving. Yields 6 servings.""]",www.cookbooks.com/Recipe-Details.aspx?id=10570,Gathered,"[""frozen corn"", ""cream cheese"", ""butter"", ""garlic powder"", ""salt"", ""pepper""]"

我尝试的:

我已经在我的本地Postgres示例上检查了这个查询,它工作正常
查询在本地postgres示例上运行良好,即localhost:5432
数据量约2 M +
数据仅为文本格式
数据库是我拥有的,它是一个基本的postgres示例heroku
PSQL脚本:

CREATE TABLE nlg_recipe (
    title VARCHAR(255) NULL,
    ingredients TEXT NULL,
    directions TEXT NULL,
    link TEXT,
    source VARCHAR(100),
    NER text NULL
);
-- not working
COPY nlg_recipe FROM '/Users/Container/data_sets/nlg_recipe_dataset.csv' DELIMITER ',' CSV HEADER;
-- working
INSERT INTO nlg_recipe (title, ingredients, directions, link, source, NER)
VALUES 
('Chicken Alfredo Pasta', 
 '250g fettuccine pasta, 1 cup cooked chicken, 1 cup heavy cream, 1/2 cup grated Parmesan cheese, salt and pepper to taste', 
 '1. Cook pasta according to package instructions. 2. In a saucepan, combine heavy cream and Parmesan cheese. 3. Add cooked chicken and season with salt and pepper. 4. Toss the cooked pasta in the sauce. 5. Garnish with extra Parmesan and serve.', 
 'https://example.com/chicken-alfredo-pasta', 
 'Allrecipes', 
 'Chicken Alfredo, Fettuccine Pasta, Heavy Cream, Parmesan Cheese, Salt, Pepper');

brjng4g3

brjng4g31#

由于您没有提供您正在使用的确切脚本,因此这里是一个通用的回复。
COPY命令需要运行DB的机器上的超级用户权限。psql \copy命令运行在您的本地机器上,不需要超级用户权限。如果您可以使用psql和\copy命令而不是DBeaver,这应该可以解决问题。
如果您必须使用DBeaver,那么您可以使用导入向导。这样查找导入向导:在左手DB导航器中,导航到要导入数据的表。右键单击表,选择“导入数据”,然后按照向导操作。

  • 根据已编辑的问题进行编辑:*

然后,您所要做的就是在COPY命令之前添加一个反斜杠-如果您从psql运行它,则COPY nlg_recipe FROM...将变为\COPY nlg_recipe FROM...
要在psql中运行的完整命令:

\COPY nlg_recipe FROM '/Users/Container/data_sets/nlg_recipe_dataset.csv' DELIMITER ',' CSV HEADER;

字符串

相关问题