Reputation: 1603
I have the following problem, I need to put in a script that is going to run before the new version is rolled the SQL code that enables the pgAgent in PostgreSQL. However, this code should be run on the maintenance database (postgres) and the database where we run the script file is another one.
I remember that in SQL Server there is a command "use " so you could do something like:
use foo
-- some code
use bar
-- more code
is there something similar in PostgreSQL?
Upvotes: 23
Views: 50223
Reputation: 331
You can put in your file something like:
\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
Upvotes: 17
Reputation: 1603
well after looking on the web for some time I found this which was what I need it http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html
Upvotes: 2
Reputation: 30324
PostgreSQL doesn't have the USE command. You would most likely use psql with the --dbname option to accomplish this, --dbname takes the database name as a parameter. See this link for details on the other options you can pass in you will also want to check out the --file option as well. http://www.postgresql.org/docs/9.0/interactive/app-psql.html
Upvotes: 1
Reputation: 8286
You can't switch databases in Postgres in this way. You actually have to reconnect to the other database.
Upvotes: 3
Reputation: 27124
Are you piping these commands through the psql
command? If so, \c databasename
is what you want.
Upvotes: 3