malejpavouk
malejpavouk

Reputation: 4445

Get all database names through JDBC

Is there any way how to get all database names out of a postgres database using JDBC? I can get the current one, but thats not what I am looking for...

I have a jUnit rule, which creates database for each test and after the test it drops it, but in some special cases, when the JVM dies, the drop never happens. So I'd like to check in the rule also existing database and clean some, which are not used any more. What I am looking for is some \l metacommand (but I can't easily ssh to the machine from unit tests...)

What would be also a solution for me would be some database ttl, something like some amqp queues have, but I suppose thats not in postgres either...

Thanks

Upvotes: 0

Views: 605

Answers (1)

user330315
user330315

Reputation:

Just run:

select datname
from pg_database

through JDBC. It returns all databases on the server you are connected to.


If you know how to get the information you want through a psql meta command (e.g. \l) just run psql with the -E switch - all internal SQL queries for the meta commands are then printed to the console.

-l actually uses a query that is a bit more complicated, but to only the the names, the above is sufficient

Upvotes: 2

Related Questions