First, it exports schema file from PostgreSQL with "pg_dump --schema-only". Then, parses the schema file for separating the each object types into separate folders and each object into a separate file. You can also parse a previously extracted pg_dump file with this script by making minor changes.
The code consists of 3 parts.
- Extracting the schema file with PostgreSQL / pg_dump
- Parsing objects to folders and files with the Bash script
- Archiving all files and converting them into a single file
You can change any of these parts or use any you want.
Detailed Steps;
- Download postgresql.schema.parse.sh file to any folder of you
- Edit database configuration part in the top of the file
- Open a terminal window from the folder
- Run "bash postgresql.schema.parse.sh"
- You will find a dated tar.gz archive file on the same folder
- You can keep this archive file regularly for schema backup
- Extract this archive to a folder for examine any file in it
- You can compare local and prod schema folders with "Meld" folder compare feature