背景:
我在Ubuntu 14.04上使用PostgreSQL 9.3.5。
在一个糟糕的脚本之后,我有一个表,我需要从通过pg_dump创建的转储文件中恢复。在此表中,我有一个基于的审计触发器 这个wiki page.如您所见,触发器函数使用hstore。
错误:
当我尝试恢复时,我得到:
$ pg_restore -a --dbname=a193 -Fc --host=localhost --port=5434 --username=postgres -W --table=foo ~/tmp/a193.dump
Password:
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 4600; 0 26146 TABLE DATA foo u2su8s81ul0a52
pg_restore: [archiver (db)] COPY failed for table "foo": ERROR: type "hstore" does not exist
LINE 6: h_old hstore;
扩展肯定存在。
=> \dx
List of installed extensions
+--------------------+---------+------------+--------------------------------------------------------------+
| Name | Version | Schema | Description |
+--------------------+---------+------------+--------------------------------------------------------------+
| dblink | 1.1 | public | connect to other PostgreSQL databases from within a database |
| hstore | 1.2 | public | data type for storing sets of (key, value) pairs |
| isn | 1.0 | public | data types for international product numbering standards |
| pg_stat_statements | 1.1 | public | track execution statistics of all SQL statements executed |
| pgcrypto | 1.0 | public | cryptographic functions |
| plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language |
| plpythonu | 1.0 | pg_catalog | PL/PythonU untrusted procedural language |
| postgres_fdw | 1.0 | public | foreign-data wrapper for remote PostgreSQL servers |
| uuid-ossp | 1.0 | public | generate universally unique identifiers (UUIDs) |
+--------------------+---------+------------+--------------------------------------------------------------+
(9 rows)
我可以在查询中使用它(作为postgres用户 - 与我上面用于恢复的角色相同):
=> select current_user;
+--------------+
| current_user |
+--------------+
| postgres |
+--------------+
(1 row)
=> \du
List of roles
+----------------+------------------------------------------------+-----------+
| Role name | Attributes | Member of |
+----------------+------------------------------------------------+-----------+
| postgres | Superuser, Create role, Create DB, Replication | {} |
| u2su8s81ul0a52 | | {} |
+----------------+------------------------------------------------+-----------+
=> select 'a=>1'::hstore;
+----------+
| hstore |
+----------+
| "a"=>"1" |
+----------+
(1 row)
问题:
答案 0 :(得分:0)
它似乎是pg_dump或pg_restore中的错误。根据Richard Huxton的建议,我恢复了一个档案。
pg_restore --data-only --table=foo -f ~/tmp/foo.sql ~/tmp/a193.dump
当我查看内容时,我发现它在顶部执行了以下操作:
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = myschema, pg_catalog;
使用\i
从psql内部运行此行仍然失败,但编辑最后一行以包含公共模式(这是安装hstore的地方)。
SET search_path = myschema, pg_catalog, public;
然后我可以使用\i
从psql内部运行并导入丢失的数据。
答案 1 :(得分:0)
我在公开定义的两个函数(checksum
和is_valid
)和表(master_values
)中遇到了同样的问题。这里checksum
正在呼叫is_valid
,而master_values
具有检查约束:
"master_values_master_id_check" CHECK(is_valid(master_id))"
请注意,在任何这些中都没有使用search_path
或架构引用。
在尝试还原转储时,我在还原过程中得到了这个信息:
pg_restore: [archiver (db)] COPY failed for table "master_values": ERROR: function checksum(integer) does not exist
奇怪的是,还原后,函数和表都已存在并且可以按预期工作。 唯一丢失的是master_values
中未恢复的数据。
这通过为search_path
指定is_valid
来解决:
ALTER FUNCTION is_valid SET search_path = public;
有关此的更多信息,请参见: