Skip to Content
Jul 4, 2016
The following script will help you change the owner of all tables and sequences in your Postgres database using the official postgres Docker image. It generates SQL with ALTER statements in the first postgres Docker container based on your DB tables and sequences and pipes the generated SQL to the second Docker container, where the modifications are run.

export user="your_new_owner"
export dbname="your_db_name"
cat <<EOF | docker run -i --rm --link postgres:postgres postgres sh -c "psql -h \$POSTGRES_PORT_5432_TCP_ADDR -p \$POSTGRES_PORT_5432_TCP_PORT -U postgres -d $dbname" | grep ALTER | docker run -i --rm --link postgres:postgres postgres sh -c "psql -h \$POSTGRES_PORT_5432_TCP_ADDR -p \$POSTGRES_PORT_5432_TCP_PORT -U postgres -d $dbname"
SELECT 'ALTER TABLE '||schemaname||'.'||tablename||' OWNER TO $user;'
FROM pg_tables WHERE schemaname = 'public';
SELECT 'ALTER SEQUENCE '||relname||' OWNER TO $user;' FROM pg_class WHERE relkind = 'S';

Also posted here:


Join in on the conversation with Vojtech Vitek when you subscribe to Vojtech's IT Blog.