Skip to content

Commit bd76b8b

Browse files
author
Frederic de Zorzi
committed
ready to publish
1 parent 7e66d27 commit bd76b8b

5 files changed

Lines changed: 33 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "pg_dump_tool"
2+
name = "pg-dump2insert"
33
version = "0.1.0"
44
authors = ["Frederic de Zorzi <fredz@pimentech.fr>"]
55

README.rst

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
PG-Inserts : Get INSERT commands from PostgreSQL dump with COPY statements
2-
==========================================================================
3-
4-
PostgreSQL's pg_dump with COPY statements is the most effective way to dump your
5-
database, but is it difficult to grep in to recover specific data.
1+
pg-dump2insert : Fast PostgreSQL COPY dump to INSERT commands converter
2+
=======================================================================
63

4+
PostgreSQL's pg_dump with *COPY* statements format is the most effective way to
5+
backup and restore your database. However for retrieving specific data or to
6+
inject dump into non-PostgreSQL database, *INSERT* commands are far more
7+
convenient. This tool simply converts *COPY* dumps to *INSERT* dumps.
78

89
Installation
910
------------
@@ -17,16 +18,22 @@ it, then type::
1718
Usage
1819
-----
1920

20-
Just pipe dump output to pg-inserts::
21+
Just pipe dump output to pg-dump2insert::
2122

22-
cat dump.sql | pg-inserts
23+
cat dump.sql | pg-dump2insert
2324

2425
If the dump file is a binary archive, use pg_restore::
2526

26-
pg_restore dump.db | pg-inserts
27+
pg_restore dump.db | pg-dump2insert
28+
pg_restore -t tablenamedump.db | pg-dump2insert
2729

2830

2931
Notice
3032
------
3133

32-
A Python version is also provided (7.5 times slower than Rust version).
34+
The only difference with "--inserts" dumps is that all numeric values are quoted
35+
in INSERTs (which is permitted in PostgreSQL), i.e.::
36+
37+
INSERT INTO tablename (id, ...) VALUES ('2', ...);
38+
39+
A (5 times slower) Python version is also provided.
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
#!/usr/bin/env python
22
import fileinput
33
import re
4-
5-
mode = "clone"
64
start_table_pattern = re.compile("^COPY (\w+) \(([\w, ]+)\) FROM stdin;")
5+
table_name = None
6+
fields = None
7+
insert_mode = False
8+
79
for line in fileinput.input():
8-
if mode == "clone":
10+
if insert_mode:
11+
if line == "\\.\n":
12+
insert_mode = False
13+
continue
14+
values = [ v == '\\N' and 'NULL' or "'%s'" % v \
15+
for v in line[:-1].replace("'", "''").split("\t") ]
16+
print "INSERT INTO %s (%s) VALUES (%s);" \
17+
% (table_name, fields, ", ".join(values))
18+
else:
919
start_table = start_table_pattern.match(line)
1020
if start_table:
1121
table_name = start_table.group(1)
1222
fields = start_table.group(2)
13-
mode = "insert"
23+
insert_mode = True
1424
continue
1525
print line,
16-
else:
17-
if line == "\\.\n":
18-
mode = "clone"
19-
continue
20-
values = [ v == '\\N' and 'NULL' or "'%s'" % v for v in line[:-1].split("\t") ]
21-
print "INSERT INTO %s (%s) VALUES (%s);" % (table_name, fields, ", ".join(values))
2226

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
else {
2020
let mut values = String::new();
2121
line.pop();
22-
for s in line.split("\t") {
22+
for s in line.replace("'", "''").split("\t") {
2323
if s == "\\N" {
2424
values += "NULL, ";
2525
} else {

0 commit comments

Comments
 (0)