如何解决 Postgresql 8.4 错误 'language “plpgsql” already exists'?

How to resolve Postgresql 8.4 error 'language “plpgsql” already exists'?

创建数据库后,我将通过脚本使用 .backup 文件在这个 blank/new 数据库上恢复 PostgreSQL 8.4 中的数据库(使用适当的错误处理) .如果发生任何错误,脚本将中止整个过程并将该过程标记为失败。在数据库还原期间生成以下错误:

pg_restore: connecting to database for restore
pg_restore: creating SCHEMA public
pg_restore: creating COMMENT SCHEMA public
pg_restore: creating PROCEDURAL LANGUAGE plpgsql
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 302; 2612 16386 PROCEDURAL LANGUAGE plpgsql postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  language "plpgsql" already exists
    Command was: 
CREATE PROCEDURAL LANGUAGE plpgsql;
pg_restore: setting owner and privileges for SCHEMA public
pg_restore: setting owner and privileges for COMMENT SCHEMA public
pg_restore: setting owner and privileges for ACL public
pg_restore: setting owner and privileges for PROCEDURAL LANGUAGE plpgsql
WARNING: errors ignored on restore: 1
Exit code : 1

我知道,这个错误在 PostgreSQL 中应该被忽略 8.x,但是由于我是通过脚本执行的,所以这个问题需要解决,即 PostgreSQL 的退出代码应该是 0,否则,整个过程不会完成。

知道怎么做吗?

我怀疑是否存在优雅的解决方法。每个人都说应该忽略该消息。

一个肮脏的解决方法:在 pg_restore 之前插入一个 DROP LANGUAGE .. 并在之后插入一个 CREATE OR REPLACE LANGUAGE (后者,以防转储不包含 CREATE 语句)

在处理这个问题几个小时后,我发现以下解决方案是基本且简单的。使用命令 'CREATE DATABASE' 创建的数据库默认使用名为 'template1' 的标准系统数据库。相反,使用 'template0'。如 document 中所述:

By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without any conflicts with objects that might have been added to template1 later on.