diff --git a/README.md b/README.md index be02f81..13b4fe5 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,12 @@ npx pg-anonymizer postgres://user:secret@localhost:1234/mydb -o dump.sql ☝️ This command requires `pg_dump`. It may already be installed as soon as PostgreSQL is installed. +Output can also be stdout ('-') so you can pipe the output to zip, gz, or to psql: + +```bash +npx pg-anonymizer postgres://user:secret@localhost:1234/mydb -o - | psql DATABASE_URL +``` + ### Specify list of columns to anonymize Use `--list` option with a comma separated list of column name: diff --git a/src/index.ts b/src/index.ts index b112380..1731211 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,7 +56,6 @@ class PgAnonymizer extends Command { char: "m", description: "Obsolete, not needed any more: max memory used to get output from pg_dump in MB", - default: "0", }), }; @@ -71,7 +70,7 @@ class PgAnonymizer extends Command { ? require(path.join(process.cwd(), flags.extension)) : null; - console.log("Launching pg_dump"); + console.error("Launching pg_dump"); const pg = spawn("pg_dump", [args.database]); pg.on("exit", function (code) { if (code != 0) { @@ -114,9 +113,16 @@ class PgAnonymizer extends Command { let indices: Number[] = []; let cols: string[] = []; - console.log("Command pg_dump started, running anonymization."); - console.log("Output file: " + flags.output); - let out = fs.createWriteStream(flags.output); + console.error("Command pg_dump started, running anonymization."); + + let out: any; + if (flags.output === "-") { + out = process.stdout; + console.error("Output to stdout"); + } else { + out = fs.createWriteStream(flags.output); + console.error("Output file: " + flags.output); + } const inputLineResults = readline.createInterface({ input: pg.stdout, @@ -126,7 +132,7 @@ class PgAnonymizer extends Command { for await (let line of inputLineResults) { if (line.match(/^COPY .* FROM stdin;$/)) { table = line.replace(/^COPY (.*?) .*$/, "$1"); - console.log("Anonymizing table " + table); + console.error("Anonymizing table " + table); cols = line .replace(/^COPY (?:.*?) \((.*)\).*$/, "$1") @@ -143,11 +149,11 @@ class PgAnonymizer extends Command { }, []); if (indices.length) - console.log( + console.error( "Columns to anonymize: " + cols.filter((v, k) => indices.includes(k)).join(", ") ); - else console.log("No columns to anonymize"); + else console.error("No columns to anonymize"); } else if (table && line.trim() && (line !== "\\.")) { line = line .split("\t")