PG备份恢复:multiple primary keys for table "t1" are not allowed

简介: PG备份恢复:multiple primary keys for table "t1" are not allowed

数据库导入导出是最常用的功能之一。PostgreSQL的备份工具可以使用pg_dump及pg_dumpall。可以通过pg_dump --help获取其使用方法。这里不对其做过多介绍。主要介绍在使用pg_dump及恢复过程中遇到的一个问题。

1、问题

使用pg_dump -c导出后,通过psql导入时报下面的错误:


ERROR:  relation "t1" already exists
ERROR:  duplicate key value violates unique constraint "t1_pkey"
ERROR:  multiple primary keys for table "t1" are not allowed

2、导入导出的操作


pg_dump -U postgres -d yzs -Fa -c -C -f all.sql
psql < all.sql

3、问题分析

1)通过-c导出时在重建database前先drop

2)通过-C导出时导出时导出create database语句

3)每次导入时,虽然库中已有导入的表结构和部分数据,及先执行drop database语句清空,应该不会出现表已存在等错误,但是这种错误确实出现了。原来,在导入时,只要已有连接连着这个database,drop语句就不会执行成功,导致清理数据库失败,后续执行对应语句时会报已存在、重复键、多个主键等错误。

4、解决方法

1)pg_dump导出时,没有选项使导出的语句中带if not exists,不能使之不存在时再创建或插入。

2)保证没有业务连接数据库时才导入,或向一个干净的数据库进行导入

目录
打赏
0
0
0
0
353
分享
相关文章
|
10月前
|
db.oplog.rs.find({"ns": "your_database_name.your_collection_name", "o": {$exists: true}}).sort({$natural: -1}).limit(1)
【5月更文挑战第22天】db.oplog.rs.find({"ns": "your_database_name.your_collection_name", "o": {$exists: true}}).sort({$natural: -1}).limit(1) 的作用
67 6
change backup ... for db_unique_name不同步到control file
change backup … for db_unique_name 可以改变备份集所属的db_unique_name,但oracle官方文档里面没有说会不会同步到db_unique_name对应的数据库的control file。我自己测试发现不会同步到control file。
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
4327 0
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
1608 0
Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails
Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails
291 0
backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.
昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示:   yMaint.ShrinkLog   Log Shrink   --  ============================================================...
1114 0
Replication-Replication Distribution Subsystem: agent xxxxxx failed. Column names in each table must be unique
原文:Replication-Replication Distribution Subsystem: agent xxxxxx failed. Column names in each table must be unique   最近遇到一个关于发布订阅(Replication)的奇葩问题,特此记录一下这个案例。
1047 0
MySQL GR(group replication) Single Primary
组复制是一种可用于实现容错系统的技术。复制组是一个通过消息传递相互交互的server集群。通信层提供了原子消息和完全有序信息交互等保障机制。MySQL组复制以这些功能和架构为基础,实现了基于复制协议的多主更新。
5032 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等