I need a SQL query to find the names of existing databases.
For people where "sys.databases" does not work, You can use this aswell;
SELECT DISTINCT TABLE_SCHEMA from INFORMATION_SCHEMA.COLUMNS
Another to add to the mix:
EXEC sp_databases
Here is a query for showing all databases in one Sql engine
Select * from Sys.Databases
I don't recommend this method... but if you want to go wacky and strange:
EXEC sp_MSForEachDB 'SELECT ''?'' AS DatabaseName'
or
EXEC sp_MSForEachDB 'Print ''?'''
SELECT name
FROM sys.databases
You'll only see the databases you have permission to see.
SELECT datname FROM pg_database WHERE datistemplate = false
#for postgres