To check your SQL version via the command line, the most universal method is to log into your database client and execute the command SELECT VERSION();. Depending on the specific database engine (MySQL, PostgreSQL, SQL Server, or SQLite), the exact command or the flag used to launch the client may vary. This process allows you to verify the exact build and release version of your database server, which is critical for ensuring compatibility with new software updates, debugging syntax errors, or auditing security patches.
What You'll Need
- Access to a terminal or command prompt (CMD on Windows, Terminal on macOS or Linux).
- The database client software installed on your machine (e.g., mysql-client, psql, sqlcmd, or sqlite3).
- Administrative or user credentials (username and password) with permission to connect to the server.
- The network address (IP or hostname) and port number if the database is hosted on a remote server.
Step-by-Step
Because different SQL dialects use different tools, follow the section that corresponds to the database software you are using. Note that menu labels and command flags may differ slightly by version, operating system, or region.
Method 1: MySQL and MariaDB
- Open your terminal or command prompt.
- Enter the following command to log in:
mysql -u username -p. Replace "username" with your actual database username. - Press Enter. The system will prompt you for your password. Type it in (the characters will not appear as you type for security) and press Enter again.
- Once you see the
mysql>prompt, type the following SQL query:SELECT VERSION(); - Press Enter. The server will return a table containing the version string (e.g., 8.0.32).
- Alternatively, if you do not want to log in fully, you can run the version check directly from the shell using the flag:
mysql --version. This shows the client version, which usually matches the server version if they were installed together.
Method 2: PostgreSQL
- Open your terminal or command prompt.
- Connect to the database using the psql utility:
psql -U username -d database_name. - Enter your password when prompted.
- At the
postgres=#prompt, execute the command:SELECT version(); - Press Enter. PostgreSQL will return a detailed string containing the version number, the operating system it was compiled for, and the compiler used.
- To exit the prompt, type
\qand press Enter. - If you are outside the database and just need the client version, type
psql --versionin your terminal.
Method 3: Microsoft SQL Server (MSSQL)
- Open the Command Prompt (CMD) or PowerShell.
- Connect to the server using the sqlcmd utility:
sqlcmd -S server_name -U username -P password. (If using Windows Authentication, you can omit the -U and -P flags). - Once connected and seeing the
1>prompt, type:SELECT @@VERSION; - On a new line, type
GOand press Enter. In SQL Server, theGOcommand is required to signal the end of a batch of statements. - The output will display the full version, build number, and the operating system version.
- Type
QUITto exit the utility.
Method 4: SQLite
- Open your terminal or command prompt.
- Launch the SQLite command line interface by typing
sqlite3. If you are opening a specific database file, typesqlite3 filename.db. - At the
sqlite>prompt, type:SELECT sqlite_version(); - Press Enter to see the version number.
- Alternatively, you can check the version from the shell without entering the prompt by typing
sqlite3 --version.
Common Mistakes
One of the most frequent errors is forgetting the semicolon (;) at the end of the SQL query. In MySQL and PostgreSQL, the command will not execute until a semicolon is detected; instead, the prompt will simply move to a new line, waiting for you to finish the statement. If you see a blank line or a continuation character, simply type a semicolon and press Enter.
Another common mistake is confusing the client version with the server version. Running mysql --version or psql --version in the terminal tells you the version of the software installed on your local computer (the client). However, the database server might be hosted on a remote machine running a completely different version. To be 100% certain of the server's version, you must log in and run the SELECT VERSION(); query.
In Microsoft SQL Server, users often forget the GO command. Unlike other SQL dialects where the semicolon or the Enter key triggers execution, sqlcmd requires GO to process the preceding block of code. Without it, the terminal will remain idle, and no results will be displayed.
Tips
If you are working in a production environment and do not have the credentials to log in as a superuser, you can still usually run the version check. The VERSION() function is typically a read-only operation that does not require high-level administrative privileges.
For those using Linux servers, you can often find the version information without logging into the SQL prompt by using the package manager. For example, running dpkg -l | grep mysql on Debian/Ubuntu or rpm -qa | grep mysql on CentOS/RHEL will show the version of the package installed on the system. This is a fast way to check the version if you have SSH access but have forgotten the database password.
When documenting your environment for a developer or a support ticket, always provide the full output of the version command rather than just the major version number. For instance, saying "MySQL 8" is less helpful than "MySQL 8.0.32," as specific bugs and features are often tied to the minor release or the specific build number.
Troubleshooting
If you receive a "command not found" error (e.g., bash: mysql: command not found), it means the database client is either not installed or its location is not included in your system's PATH environment variable. To fix this, you can either install the client tools or navigate directly to the folder where the executable is located (e.g., C:\Program Files\MySQL\MySQL Server 8.0\bin on Windows) and run the command from there.
If you encounter an "Access Denied" or "Authentication Failed" error, double-check your username and password. Ensure that the user account you are using has the permission to connect from your specific IP address. Many databases are configured to only allow connections from localhost for security reasons. If you are connecting remotely, you may need to update the server's bind-address or grant permission to your IP via the GRANT command.
In cases where the connection times out, verify that the database service is actually running. On Linux, you can check this with systemctl status mysql or systemctl status postgresql. If the service is stopped, start it using systemctl start [service_name]. Additionally, check if a firewall is blocking the port (typically 3306 for MySQL, 5432 for PostgreSQL, and 1433 for SQL Server).
Frequently Asked Questions
Can I check the SQL version without a password?
Generally, no. You need a valid user account to execute queries on the server. However, if you have root or sudo access to the operating system where the server is installed, you can use the package manager (like apt or yum) to see which version is installed.
Is the client version always the same as the server version?
No. It is very common to have a newer version of the client tool installed on your laptop while connecting to an older version of the database server hosted in the cloud. Always use a SQL query to verify the server version.
What is the difference between @@VERSION and VERSION()?
VERSION() is a function used primarily in MySQL and PostgreSQL. @@VERSION is a global variable used in Microsoft SQL Server to retrieve the same information. They achieve the same goal but are specific to their respective languages.
Why does my version number have a letter at the end?
Letters in version strings often denote a specific release channel. For example, "rc" stands for Release Candidate, and "beta" indicates a pre-release version. A number without a letter usually indicates a stable, general availability (GA) release.
Does checking the version slow down the database?
No. Checking the version is a lightweight metadata request. It does not scan tables or lock resources, so it is safe to run even on high-traffic production servers.