用于在cmd上恢复pgsl备份的psql错误

时间:2016-11-22 17:05:41

标签: sql postgresql cmd backup restore

我很难理解这个错误的含义。我使用的命令是:

psql -U postgres -d app -1 -f postgres.sql

这是错误:

 psql:postgres.sql:1879: ERROR:  current transaction is aborted, commands ignored
     until end of transaction block

ROLLBACK
psql:postgres.sql:0: WARNING:  there is no transaction in progress

不确定如何使交易正在进行中。这是我尝试导入postgresl的sql文件:http://pastebin.com/2xMGhstd

2 个答案:

答案 0 :(得分:0)

正如joop所解释的那样,你的SQL文件不一致。

raffle.user_id"user".id存在外键约束,这意味着对于raffle.user_id中的每个值,"user"中必须有一行id具有相同的值。

现在"user"中没有插入id等于1的行,但脚本会尝试在raffle中插入一行,user_id等于1。

违反外键约束导致错误。一旦PostgreSQL事务中出现错误,您所能做的就是ROLLBACK。在您这样做之前,事务中的所有语句都将因您观察到的错误而失败。

您拥有的唯一解决方案是修复数据以使其保持一致,或者通过删除外键约束来放弃一致性。

备注:选择USER之类的保留SQL关键字作为名称是个坏主意。

答案 1 :(得分:0)

正如@joop评论的那样:你的.dmp文件已经瘫痪了,因为users表中不存在user_id = 1,但在raffle表中引用了该文件。 示例:运行下一个代码段,然后取消注释该用户#1的行并重新运行。

-- Drop the schema *after* usage
-- DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;

CREATE TABLE users (
        id INTEGER NOT NULL,
        email VARCHAR(120),
        PRIMARY KEY (id),
        UNIQUE (email)
);
    -- UNCOMMENT the next line to also add user#1
-- INSERT INTO users VALUES(1,'Jim.Fake@nolive.co.uk');
INSERT INTO users VALUES(2,'osman.narnia@live.co.uk');
INSERT INTO users VALUES(3,'KimFake1@outlook.com');
INSERT INTO users VALUES(4,'jaakume@gmail.com');
INSERT INTO users VALUES(5,'omarblack@protonmail.com');
INSERT INTO users VALUES(6,'osman.everton@hotmail.com');
INSERT INTO users VALUES(7,'radoslaw@ganczarek.in');
INSERT INTO users VALUES(8,'kane1001@live.co.uk');
INSERT INTO users VALUES(9,'osman.soloking009@outlook.com');
INSERT INTO users VALUES(10,'Shum1945@fleckens.hu');
CREATE TABLE raffle (
        id INTEGER NOT NULL,
        user_id INTEGER,
        colour VARCHAR(120),
        up1 VARCHAR(4),
        up2 VARCHAR(4),
        PRIMARY KEY (id),
        CONSTRAINT _color_up1_up2_uc UNIQUE (colour, up1, up2),
        FOREIGN KEY(user_id) REFERENCES users (id)
);
INSERT INTO raffle VALUES(1,1,'Blue','7c4c','5c7e');
INSERT INTO raffle VALUES(2,1,'Pink','635d','853f');
INSERT INTO raffle VALUES(3,1,'Plum','5e80','7611');
INSERT INTO raffle VALUES(4,1,'Aqua','937c','1b75');
INSERT INTO raffle VALUES(5,2,'Navy','1d9a','8914');
INSERT INTO raffle VALUES(6,1,'Grey','d869','fc97');
INSERT INTO raffle VALUES(7,4,'Rose','5fee','b31f');
INSERT INTO raffle VALUES(8,1,'Ruby','d5b4','e749');
INSERT INTO raffle VALUES(9,2,'Teal','cf0b','3bf5');
INSERT INTO raffle VALUES(10,1,'Gold','98a7','3079');
INSERT INTO raffle VALUES(11,1,'Jade','5c69','66f8');
INSERT INTO raffle VALUES(12,1,'Lime','156f','6b34');
INSERT INTO raffle VALUES(13,1,'Blue','7da3','d95b');
INSERT INTO raffle VALUES(14,1,'Pink','a63e','b9b6');
INSERT INTO raffle VALUES(15,1,'Plum','d989','71a5');
INSERT INTO raffle VALUES(16,1,'Aqua','7372','0682');
/****
INSERT INTO raffle VALUES(17,9,'Navy','01b3','e444');
INSERT INTO raffle VALUES(18,1,'Grey','d679','0123');
INSERT INTO raffle VALUES(19,1,'Rose','5963','692d');
...
***/
COMMIT;