To list all databases in SQL, enter SHOW DATABASES; (MySQL/MariaDB) or SELECT datname FROM pg_database; (PostgreSQL) in the command-line client. For SQL Server, use SELECT name FROM sys.databases;—each command requires the correct database server and client already installed and running.
What You'll Need
- A working SQL command-line interface (e.g., MySQL CLI, psql for PostgreSQL, or sqlcmd for SQL Server)
- The appropriate database server running and accessible (MySQL, PostgreSQL, or SQL Server)
- Basic permissions to query system metadata (or admin privileges if the account is restricted)
Step-by-Step
- Launch your SQL command-line client
- For MySQL/MariaDB: Open a terminal and type
mysql -u [username] -p(replace [username] with your account). You’ll be prompted for a password. - For PostgreSQL: Use
psql -U [username] -d [database](replace [username] with your account and [database] with the default database if needed). Enter your password when prompted. - For SQL Server: Run
sqlcmd -S [server_name] -U [username] -P [password](e.g.,sqlcmd -S localhost -U sa). Note: SQL Server often requires Windows Authentication by default.
Note: Menu labels may vary slightly by version or region. Always refer to your server’s documentation for exact CLI syntax.
- For MySQL/MariaDB: Open a terminal and type
- Verify you’re connected to the correct server
- In MySQL, type
SHOW STATUS;. Look forVersion_commentto confirm the server version. - In PostgreSQL, run
SHOW server_version;. The output will show the PostgreSQL version. - In SQL Server, type
SELECT @@VERSION;. This displays the SQL Server version and OS details.
If the connection is wrong, exit the client (
EXIT;in MySQL/PostgreSQL, press Ctrl+Break in SQL Server) and reconnect with the correct server name. - In MySQL, type
- Run the database list command for your server type
- MySQL/MariaDB: Enter
SHOW DATABASES;. This returns a list of all databases on the server, including the defaultinformation_schemaandmysqlsystem databases. - PostgreSQL: Use
SELECT datname FROM pg_database;. This excludes system databases by default; addWHERE datname NOT LIKE 'pg_%'to filter out PostgreSQL’s internal databases. - SQL Server: Execute
SELECT name FROM sys.databases;. This lists all databases, including user-created ones and system databases likemasterandtempdb.
Example output for MySQL:
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | your_custom_db | +--------------------+
- MySQL/MariaDB: Enter
- Filter or format the output (optional)
- To exclude system databases in MySQL, add
WHERE Database NOT IN ('information_schema', 'mysql', 'performance_schema')to aSELECTquery (e.g.,SELECT Database FROM mysql.database WHERE Database NOT IN ('information_schema', 'mysql', 'performance_schema')). - In PostgreSQL, sort databases alphabetically with
SELECT datname FROM pg_database ORDER BY datname;. - In SQL Server, limit results to user databases only with
SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb') AND state = 0;.
- To exclude system databases in MySQL, add
- Exit the command line
Type
EXIT;(MySQL/PostgreSQL), press Ctrl+Break (SQL Server), or Ctrl+D (Linux/macOS) to close the session.
Common Mistakes
- Using the wrong command for the server
MySQL’s
SHOW DATABASES;won’t work in PostgreSQL, and PostgreSQL’spg_databasewon’t appear in SQL Server. Always check your server type first. - Forgetting to connect to a server
Some commands may "work" but return no results if you’re not connected to the correct server. Always verify the connection (step 2 above).
- Assuming system databases are irrelevant
Databases like
mysql(MySQL) ormaster(SQL Server) are critical for server operation. Avoid dropping them unless you understand the risks. - Ignoring permissions
If you see an error like "Access denied for user," your account lacks privileges. Contact your database administrator to grant the necessary rights.
Tips
- Use
--help(MySQL) or?(PostgreSQL) after connecting to see available commands and syntax. - For remote servers, replace
localhostwith the server’s IP or hostname (e.g.,mysql -h remote-server.com -u user). - Save frequently used commands in a script file (e.g.,
list_dbs.sql) for quick reuse. - In PostgreSQL, use
\l(a backslash meta-command) as a shortcut for listing databases. - For SQL Server, use
sqlcmd -i "your_query.sql"to run commands from a file. - If you’re unsure which server you’re using, run
SELECT @@VERSION;(SQL Server),SHOW VARIABLES LIKE 'version';(MySQL), orSELECT version();(PostgreSQL).
Troubleshooting
If listing databases fails, follow this order:
- Check the server is running
- For MySQL: Use
sudo systemctl status mysql(Linux) or check the Services app (Windows). - For PostgreSQL: Run
sudo systemctl status postgresql. - For SQL Server: Open SQL Server Management Studio (SSMS) or check the Windows Services app for the SQL Server instance.
sudo systemctl start mysql). - For MySQL: Use
- Verify connection details
- Ensure the server name, username, and password are correct.
- Test connectivity with
telnet [server_name] 3306(MySQL),telnet [server_name] 5432(PostgreSQL), ortelnet [server_name] 1433(SQL Server). If it fails, check firewalls or network settings.
- Update your client tools
- Outdated clients may not support newer syntax. Update MySQL CLI, psql, or sqlcmd to the latest version.
- Check for permission errors
- If you see "ERROR 1045 (28000): Access denied," your account lacks privileges. Either:
- Log in as an admin and grant permissions (e.g.,
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';in MySQL), or - Use the correct credentials with sufficient access.
- Reinstall the client if needed
- If commands fail with cryptic errors (e.g., "unrecognized command"), reinstall the client software (e.g., MySQL Workbench’s CLI tools or PostgreSQL’s
psql).
- If commands fail with cryptic errors (e.g., "unrecognized command"), reinstall the client software (e.g., MySQL Workbench’s CLI tools or PostgreSQL’s
- Review server logs
- MySQL logs:
/var/log/mysql/error.log(Linux) or check the Windows Event Viewer. - PostgreSQL logs:
/var/log/postgresql/postgresql-[version]-main.log. - SQL Server logs:
ERRORLOG(location varies; open SSMS to navigate to it).
Look for errors like connection timeouts, authentication failures, or syntax issues.
- MySQL logs:
Frequently Asked Questions
Can I list databases without logging in?
No. You must authenticate with valid credentials to query system metadata. Anonymous access is disabled by default for security reasons.
Why does my command return no results?
This usually means:
- You’re not connected to the correct server (e.g., running MySQL’s
SHOW DATABASESagainst a PostgreSQL server). - The server is unreachable (check network/firewall settings).
- Your account lacks permissions to view databases.
SELECT @@VERSION; (SQL Server), SHOW VARIABLES; (MySQL), or SHOW server_version; (PostgreSQL).
How do I list databases in a specific directory?
This isn’t directly supported in SQL commands, but you can:
- In MySQL/MariaDB, use
SHOW VARIABLES LIKE 'datadir';to find the data directory, then browse the filesystem manually (e.g.,/var/lib/mysql/). - In PostgreSQL, check
pg_data_directory(set viaSHOW data_directory;) and look forbase/subdirectories (each corresponds to a database). - In SQL Server, databases are stored in
MSSQL\DATA(Windows) or/var/opt/mssql/data(Linux). UseSELECT physical_name FROM sys.master_files;to find file locations.
Note: Manually editing files in the data directory can corrupt databases. Always use SQL commands for modifications.
Can I list databases remotely?
Yes, but you must:
- Enable remote connections in your server configuration:
- MySQL: Edit
/etc/mysql/mysql.conf.d/mysqld.cnfand setbind-address = 0.0.0.0. Restart MySQL withsudo systemctl restart mysql. - PostgreSQL: Edit
postgresql.confand setlisten_addresses = '*'. Restart PostgreSQL and enable peer/trust authentication inpg_hba.conf. - SQL Server: Configure SQL Server Browser service and ensure TCP/IP is enabled in
SQL Server Configuration Manager. - Use the remote server’s hostname/IP in your connection string (e.g.,
mysql -h remote-server.com -u user). - Open firewall ports (3306 for MySQL, 5432 for PostgreSQL, 1433 for SQL Server) on the server.
For security, avoid using public IPs or disable remote access when not needed.
How do I list databases in a cloud database (e.g., AWS RDS, Azure SQL)?
The commands remain the same, but you must:
- Use the cloud provider’s endpoint (e.g.,
mysql -h your-rds-endpoint.rds.amazonaws.com -u admin). - Ensure your security group allows inbound traffic on the appropriate port (3306, 5432, or 1433).
- For Azure SQL, use
sqlcmd -S your-server.database.windows.net -U adminand include the password or authenticate via Active Directory.
Cloud providers often limit direct CLI access; check their documentation for managed tools (e.g., AWS RDS Console or Azure Portal).
Is there a way to list databases without typing the full command?
Yes:
- In PostgreSQL, use the
\lmeta-command (type it directly in the psql prompt). To expand it, use\l+. - In MySQL, enable tab completion by typing
SHOW DATABASES;and pressing Tab after the semicolon to auto-complete. - In SQL Server, use
GOto execute multiple commands at once (e.g.,SELECT name FROM sys.databases GOinsqlcmd).
For long-term efficiency, save commands to a script file (e.g., list_dbs.sql) and run them with --file=your_script.sql (MySQL) or -i (SQL Server).