Postgresql invalid command \N数据恢复处理
做一个单表恢复工作,数据在1000多W,说是报了错误导不进去,环境与流程见下:
恢复步骤:
1.导出语句
pg_dump -h xxxxx -p 5432 -U postgres -b -Fp db_test -t t_kenyon -f /var/t_kenyon.bak
2.导入语句
psql -h xxxx -d new_db -U postgres < /var/t_kenyon.bak
3.报错信息,屏幕上一堆的诸如
invalid command \N
invalid command \N
invalid command \N
invalid command \N
invalid command \N
invalid command \N
........
分析处理:
因为是逻辑导出没有压缩定制的文件,故可以查看备份内容
[postgres@localhost ~]$ more t_kenyon.bak
--
-- PostgreSQL database 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 = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: t_kenyon; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE t_kenyon (
col1 integer DEFAULT nextval('t_kenyon_col1_seq'::regclass) NOT NULL,
col2 ..
col3 ..
);
ALTER TABLE public.t_kenyon OWNER TO postgres;
--
-- Name: COLUMN t_kenyon.col; Type: COMMENT; Schema: public; Owner: postgres
--
--
-- Data for Name: t_kenyon; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY t_kenyon (col1,col2,col3....) FROM stdin;
3315866 \N 1 5.00 \N \N \N 2011-01-12 08:37:07+08 1 4130000 爱我中话 \N 708 Kenyon HZ 雅安加油 HZ01 HELLO \N 9 \N
3315934 \N 1 5.00 \N \N \N 2011-01-12 09:13:17+08 1 4130000 我哎中华 \N 708 kenyon HZ 雅安加油
..........此处略去1W字
看起来报错的信息都是\N即空格的地方出错了,检查了一下postgres的日志,有几条信息发现很有意思
2013-04-23 00:16:23.149 PDT,"postgres","postgres",24738,"[local]",51763545.60a2,4,"CREATE TABLE",2013-04-23 00:16:21 PDT,2/331,1856,ERROR,42P01,"relation ""t_kenyon_col1_seq"" does not exist",,,,,,"CREATE TABLE t_kenyon (col1,col2...)..
这看起来是导数据之前的建表失败了,因为sequence不存在,后面的copy操作直接就报了N多的错误,尝试先建一下索引,再重新导入
postgres=#CREATE SEQUENCE t_kenyon_col1_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 17354062 CACHE 1;
CREATE SEQUENCE
postgres=# \q
[postgres@localhost ~]$ psql -d postgres -U postgres < /var/t_kenyon.bak
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
COMMENT
ALTER TABLE
CREATE INDEX
CREATE INDEX
[postgres@localhost ~]$
OK,导入成功。但是有一个问题,为什么pg_dump导出的时候没有把sequence带出来呢?验证一下
[postgres@localhost ~]$ psql
psql (9.2.4)
Type "help" for help.
postgres=# \d
No relations found.
postgres=# create table d_kenyon(id serial,vname varchar(30));
NOTICE: CREATE TABLE will create implicit sequence "d_kenyon_id_seq" for serial column "d_kenyon.id"
CREATE TABLE
postgres=# insert into d_kenyon(vname) select generate_series(1,10)||'Hi,Kenyon!';
INSERT 0 10
postgres=# select * from d_kenyon;
id | vname
----+--------------
1 | 1Hi,Kenyon!
2 | 2Hi,Kenyon!
3 | 3Hi,Kenyon!
4 | 4Hi,Kenyon!
5 | 5Hi,Kenyon!
6 | 6Hi,Kenyon!
7 | 7Hi,Kenyon!
8 | 8Hi,Kenyon!
9 | 9Hi,Kenyon!
10 | 10Hi,Kenyon!
(10 rows)
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------------+----------+----------
public | d_kenyon | table | postgres
public | d_kenyon_id_seq | sequence | postgres
[postgres@localhost ~]$ pg_dump -U postgres -b -Fp postgres -t d_kenyon -f d_kenyon.bak
[postgres@localhost ~]$ more d_kenyon.bak
--
-- PostgreSQL database 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 = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: d_kenyon; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE d_kenyon (
id integer NOT NULL,
vname character varying(30)
);
ALTER TABLE public.d_kenyon OWNER TO postgres;
--
-- Name: d_kenyon_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE d_kenyon_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.d_kenyon_id_seq OWNER TO postgres;
--
-- Name: d_kenyon_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE d_kenyon_id_seq OWNED BY d_kenyon.id;
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY d_kenyon ALTER COLUMN id SET DEFAULT nextval('d_kenyon_id_seq'::regclass);
--
-- Data for Name: d_kenyon; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY d_kenyon (id, vname) FROM stdin;
1 1Hi,Kenyon!
2 2Hi,Kenyon!
3 3Hi,Kenyon!
4 4Hi,Kenyon!
5 5Hi,Kenyon!
6 6Hi,Kenyon!
7 7Hi,Kenyon!
8 8Hi,Kenyon!
[postgres@localhost ~]$ psql
psql (9.2.4)
Type "help" for help.
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------------+----------+----------
public | d_kenyon | table | postgres
public | d_kenyon_id_seq | sequence | postgres
(2 rows)
postgres=# drop table d_kenyon;
DROP TABLE
postgres=# \q
[postgres@localhost ~]$ psql < d_kenyon.bak
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE SEQUENCE
ALTER TABLE
ALTER SEQUENCE
ALTER TABLE
setval
--------
10
(1 row)
[postgres@localhost ~]$
发现用serial产生的sequence是可以导出并导入的,回过头再去看异常的表,发现该表字段不是serial,模拟一下非serial字段的pg_dump导出情况
postgres=# create table d_test as select * from d_kenyon;
SELECT 10
postgres=# alter table d_test alter column id set default nextval('d_kenyon_id_seq'::regclass);
ALTER TABLE
postgres=# \d d_test
Table "public.d_test"
Column | Type | Modifiers
--------+-----------------------+----------------------------------------------
id | integer | default nextval('d_kenyon_id_seq'::regclass)
vname | character varying(30) |
postgres=# \q
[postgres@localhost ~]$ pg_dump -U postgres -b -Fp postgres -t d_test -f d_kenyon.bak
[postgres@localhost ~]$ more d_kenyon.bak
--
-- PostgreSQL database 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 = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: d_test; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE d_test (
id integer DEFAULT nextval('d_kenyon_id_seq'::regclass),
vname character varying(30)
);
ALTER TABLE public.d_test OWNER TO postgres;
--
-- Data for Name: d_test; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY d_test (id, vname) FROM stdin;
1 1Hi,Kenyon!
2 2Hi,Kenyon!
3 3Hi,Kenyon!
4 4Hi,Kenyon!
5 5Hi,Kenyon!
6 6Hi,Kenyon!
7 7Hi,Kenyon!
8 8Hi,Kenyon!
9 9Hi,Kenyon!
10 10Hi,Kenyon!
\.
--
-- PostgreSQL database dump complete
--
确实是没有sequence导出来的,查了一下,pg_depend里序列与表并没有关联上,也就是说这样的表与sequence是独立的,可以用以下SQL验证一下表与sequence的关联关系
WITH fq_objects AS (SELECT c.oid,c.relname AS fqname ,c.relkind, c.relname AS relation FROM
pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),
sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'),
tables AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' )
SELECT s.fqname AS sequence,'->' as depends,t.fqname AS table
FROM pg_depend d JOIN sequences s ON s.oid = d.objid
JOIN tables t ON t.oid = d.refobjid
WHERE d.deptype = 'a' and t.fqname = 'd_kenyon';
总结:
扩展开来想,这种备份如果是整个库备份再恢复,应是OK的,后来验证确实如此,故对于整库恢复是不用考虑这个问题的,单表恢复则需要注意一下。
Postgresql invalid command \N数据恢复处理的更多相关文章
- Apache报错信息之Invalid command 'Order', perhaps misspelled or defined by a module not included in the server config
今天配置开启Apache虚拟主机时, 然后日志报错提示: Invalid command 'Order', perhaps misspelled or defined by a module not ...
- python安装locustio报错error: invalid command 'bdist_wheel'的解决方法
locust--scalable user load testing tool writen in Python(是用python写的.规模化.可扩展的测试性能的工具) 安装locustio需要的环境 ...
- Apache Error: Invalid command ‘Allow’, perhaps misspelled or defined by a module not included in the server configuration
在一个Window Server 2008R2系统上使用Apache架设了一个PHP的网站项目 在配置Apache的过程中出现了以下问题 根据上面的提示说是没有相应的权限,那就在虚拟主机里进行了配 ...
- Python pip – error: invalid command ‘bdist_wheel’
原文@http://software-engineer.gatsbylee.com/python-pip-error-invalid-command-bdist_wheel/ Python pip – ...
- PHP 500 -Invalid command RewriteEngine的解决
转自:http://blog.csdn.net/wang02011/article/details/8205903 环境: wampserver-2.1a 系统 : win8 错误 : 500 ...
- zip error: Invalid command arguments
在编译使用svn管理的android代码时,会出现如下错误: zip error: Invalid command arguments (cannot repeat names in zip file ...
- .htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
php项目 .htaccess文件配置如下: #文件缓存时间配置 <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css)$"& ...
- sed: 1: “…”: invalid command code on Mac OS
昨天因为项目中有很多文件的同一个变量需要批量替换成另一个,想用sed做这个.Linux 这样其实就可以了 ~# sed -i “s/string_old/string_new/g” grep -rl ...
- Win7系统64位环境下使用Apache——安装Apache2.4时报错“Invalid command Order”问题的解决
之前在文章Win7系统64位环境下使用Apache--Apache2.4整合Tomcat与mod_jk提到了安装Apache2.4时有可能报错: Invalid command 'Order', pe ...
- Apache2.4启动时报AH00526错误(Invalid command 'Order')
在WIN XP下手动配置PHP环境,安装Apache2.4+fastcgi后,重启Apache服务,出现如下提示: AH00526: Syntax error on line 293 of D:/ph ...
随机推荐
- appium环境搭建(从入门到放弃)
一.appium环境搭建 1.python3 python3的下载安装这里就不多做介绍了,当然你也可以选择自己喜欢的语音,比如java.... 2.jdk 1)下载地址 官网(需登录账号): http ...
- SpringBoot 自动装配的原理分析
关于 SpringBoot 的自动装配功能,相信是每一个 Java 程序员天天都会用到的一个功能,但是它究竟是如何实现的呢?今天阿粉来带大家看一下. 自动装配案例 首先我们通过一个案例来看一下自动装配 ...
- CBV如何添加装饰器?
目录 一:CBV如何添加装饰器 1.CBV中django不建议直接给类的方法加装饰器 2.CBC添加装饰器的三种方法 3.CBV添加装饰器实战 一:CBV如何添加装饰器 1.CBV中django不建议 ...
- 无人机集群的分布式协作 VI-SLAM
以下内容来自从零开始机器人SLAM知识星球 每日更新内容 点击领取学习资料 → 机器人SLAM学习资料大礼包 论文# D2SLAM: Decentralized and Distributed Col ...
- 百倍加速IO读写!快使用Parquet和Feather格式!⛵
作者:韩信子@ShowMeAI 数据分析实战系列:https://www.showmeai.tech/tutorials/40 本文地址:https://www.showmeai.tech/artic ...
- Springboot整合策略模式概念->使用场景->优缺点->企业级实战
一.前言 策略模式可能是在工作中使用最多的,也是在面试中最常提到的,代码重构和优化的必备! 小编之前也是一直说,其实没有真正的实战:最近有了机会实战了一下,来分享一下使用心得和在企业级的使用! 二.策 ...
- python之路47 django路由层配置 虚拟环境
可视化界面之数据增删改查 针对数据对象主键字段的获取可以使用更加方便的obj.pk获取 在模型类中定义双下str方法可以在数据对象被执行打印操作的时候方便的查看 ''' form表单中能够触发提交动作 ...
- Java基础篇——JVM初步
1.JVM的位置 2.JVM体系结构 3.类加载器 虚拟机加载器(java) 启动类(根)加载器(C++) 扩展类加载器(java)↑ 应用程序加载器(java)↑ 4.双亲委派机制 类加载器收到类加 ...
- [C#]从两个例子理解async/await
1 例子1 输出的结果为: 可以看出执行的结果为:A--D--B--C--E. 我们再看下一个例子(注意,我们把代码中D和E的位置交换) 2 例子2 结果: 结果为:A--B--C--E--D 3 理 ...
- dp 优化
dp 优化 \(\text{By DaiRuiChen007}\) I. [ARC085D] - NRE \(\text{Link}\) 思路分析 将最终的第 \(i\) 对 \(a_i\) 和 \( ...