Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove comments and settings from pg_dump output?

I am trying to dump only data from a PostgreSQL database using pg_dump and then to restore those data into another one. But generating sql script with this tool also add some comments and settings into the output file.

Running this command :

pg_dump --column-inserts --data-only my_db > my_dump.sql

I get something like :

--
-- PostgreSQL database dump
--

-- Dumped from database version 8.4.22
-- Dumped by pg_dump version 10.8 (Ubuntu 10.8-0ubuntu0.18.04.1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET row_security = off;

--
-- Data for Name: sf_guard_user; Type: TABLE DATA; Schema: public; Owner: admin
--

INSERT INTO public.....

Is there any way to avoid pg_dump generating those comments and settings ? I could do a small script to remove every lines before the first insert but it also generates comments everywhere on the file and I am sure there is a cleaner way to proceed but found nothing.

like image 314
PopHip Avatar asked Jun 20 '19 08:06

PopHip


People also ask

How do you delete a comment on Pgadmin?

To remove a comment, write NULL in place of the text string. Comments are automatically dropped when their object is dropped. A SHARE UPDATE EXCLUSIVE lock is acquired on the object to be commented.

Does pg_dump delete database?

Clear database completely Note: pg_dump & pg_restore don't fully clear the entire database. DROP SCHEMA public CASCADE; CREATE SCHEMA public; If you are using PostgreSQL 9.3 or greater, you may also need to restore the default grants.

Does Pg_restore delete existing data?

Heads up: pg_restore --clean keeps existing tables. When restoring a PostgreSQL dump using pg_restore , you usually add the --clean flag to remove any existing data from tables. Note that this only removes data from tables that are part of the dump and will not remove any extra tables.

Does pg_dump lock database?

pg_dump is a utility for backing up a PostgreSQL database. It makes consistent backups even if the database is being used concurrently. pg_dump does not block other users accessing the database (readers or writers).


1 Answers

I don't think there is. I'd simply pipe through grep to filter out lines that start with the comment delimiter:

pg_dump --column-inserts --data-only my_db | grep -v "^--" > my_dump.sql
like image 50
fphilipe Avatar answered Oct 04 '22 00:10

fphilipe