Reputation: 639
How backup and restore only list(name) databases, roles and right for databases in postgresql without data?
Bash script for get list database postgresql
sudo -i -u postgres psql -t -A -c 'SELECT datname FROM pg_database'
But how restore databases and relationship for roles and databases in postgresql?
Vao Tsun answer:
to prepare sql with database list for creation use smth like: psql -c "select 'create database '||datname||';' from pg_database where not datistemplate and datname != 'postgres';" -At >cdbs.sql, to create database from a list, use psql -f cdbs.sql against new cluster
But all restored databases have owner postgres. How restore databases with owner? Thanks
Upvotes: 1
Views: 1117
Reputation: 51559
from top of the head - try:
pg_dumpall -g
pg_dump -s >to_file.sql
and then psql -f to_file.sql
ignoring creations errors.psql -c "select 'create database '||datname||';' from pg_database where not datistemplate and datname != 'postgres';" -At >cdbs.sql
, to create database from a list, use psql -f cdbs.sql
against new clusterUpvotes: 0