Reputation: 21751
Run SQL state on many database at the same time?
I HAVE DATABASE A,B,C
,
At the same I want to run the sql statments.
for example:
ALTER TABLE tbl_test ADD COLUMN col_test character varying(10);
Will do Alter to A,B,C
at the same time.
I am using PostgreSQL
.
Did anyone know how to do or write sql script
?
Upvotes: 0
Views: 519
Reputation: 2376
This post should give you an answer.
You must specify the database to use on connect; if you want to use psql for your script, you can use "\c name_database" à la:
CREATE DATABASE testdatabase;
\c testdatabase
CREATE TABLE testtable (testcolumn int);
and
Most MySQL users misunderstand "databases" in postgresql. The closest equivalent in PostgreSQL to a MySQL "database" is a PostgreSQL "schema". If you expect to be able to run queries that use data from multiple "databases" you really want to use schema.
See the help for the "psql" command for basic scripting, including the "\c" command to connect to another DB. For help on an SQL command, run "\h COMMANDNAME" in psql, or read the manual for that command.
and
IF you are using the psql command line utility to execute these scripts, then \c dbname will connect to a new database. However, if you're processing these scripts some other way, then your app will have to disconnect from the one database and connect to the other itself (this is what the \c command tells psql to do)
Upvotes: 1