简体   繁体   中英

How to present postgres schema dump in cleaner format

I want to dump my postgres schema in a more readable form. I am using pg_dump, so for the example below I run:

pg_dump -U user -p port -h host -s -t public.exchange_rates -d db_name > schema.sql

Instead of having output of the form

CREATE TABLE public.exchange_rates (
    currency_code character varying NOT NULL,
    xr_usd double precision NOT NULL,
    date_updated date,
    CONSTRAINT iso_4217_alpha CHECK ((char_length((currency_code)::text) = 3))
);


ALTER TABLE public.exchange_rates OWNER TO postgres;

--
-- Name: COLUMN exchange_rates.xr_usd; Type: COMMENT; Schema: public; Owner: postgres
--

COMMENT ON COLUMN public.exchange_rates.xr_usd IS 'the number of USD you can buy for 1 unit of the currency';


--
-- Name: exchange_rates exchange_rates_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.exchange_rates
    ADD CONSTRAINT exchange_rates_pkey PRIMARY KEY (currency_code);

The output would be of the form

CREATE TABLE public.exchange_rates(
    currency_code VARCHAR NOT NULL PRIMARY KEY,
    xr_usd double precision NOT NULL,
    date_updated DATE
)

Ie contains the table information as you would if creating the table(s) from scratch or in a more human readable format

A partial solution (which isn't quite what I want) would be something like

psql -U user -p port -h host -c "\d exchange_rates" > schema.txt

which does give a human readable display, but not in the CREATE TABLE style format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM