1

I have 50 databases. All the table schema in all the databases are the same. If I have a new column to be added to one table, I will have to run 50 times to update in all databases. This is very difficult all the time.

Is there an editor for postgres where there is a way to execute a query in all databases?

Currently, I am using dbeaver. I have to change the connection all the time and execute the query. Please help.

Example: I had to alter a table by adding a column. ALTER TABLE table-name ADD new-column-name column-definition;

It is very difficult to execute for 50 databases.

2
  • 1
    You might consider using a database schema migration tool such as Flyway or Liquibase. Commented Jan 18, 2020 at 1:10
  • 3
    One approach is to use scripts to automate the process, such as a bash script or batch file to make each connection in turn and then use psql to run the ALTER TABLE script. Commented Jan 18, 2020 at 2:18

1 Answer 1

2

For executing this answer, you need to have dblink extension.

DO $$
DECLARE
i text;
BEGIN

FOR i in SELECT datname FROM pg_database
LOOP
 EXECUTE 'select * from dblink(''host=localhost
     user=postgres
     password=postgres
     dbname='||i||' port=5432'',''ALTER TABLE table-name ADD new-column-name column-definition'' ) tt(
     updated text)'
END LOOP;
END $$
Sign up to request clarification or add additional context in comments.

2 Comments

Should dblink be created already? Please clarify. @Bharti Mohane
you can create extension dblink in postgres, you can try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.