To get out of the SQL command line, type exit; or quit; and press Enter. This ends your session and returns you to the operating system prompt. No additional tools or permissions are needed beyond having an active SQL client session.
What You'll Need
- An active SQL command-line session (such as mysql, psql, sqlcmd, or sqlite3)
- Keyboard access to type commands
- Basic familiarity with the SQL client you're using
Step-by-Step
- Ensure the SQL command line is active and waiting for input (you should see a prompt like
mysql>,#, orsql>). - Type
exit;exactly as shown, including the semicolon. This is the standard command to terminate the session in most SQL clients. - Press the Enter key to execute the command.
- Wait for the session to close — you should see a confirmation message like
Bye,Goodbye, or a return to your system’s shell prompt (e.g.,$orC:\>). - If the session does not close, repeat the command or try
quit;as an alternative. - Once exited, verify you are back in your operating system’s command prompt before proceeding with other tasks.
Common Mistakes
- Forgetting the semicolon: Typing
exitorquitwithout the semicolon may not work in clients like MySQL or PostgreSQL, which require it to end a statement. - Confusing SQL commands with shell commands: Trying to use
exitwithout the semicolon in a shell that’s already inside SQL can lead to no response or errors. - Assuming
Ctrl+Calways works: WhileCtrl+Ccan interrupt a query, it doesn’t reliably terminate the SQL session itself and may leave you in a broken state. - Closing the terminal window abruptly: This can leave database connections open or cause issues with transaction logs; always use the proper exit command first.
- Using GUI-specific shortcuts: Shortcuts like
Alt+F4or menu-based exits don’t apply in pure command-line environments.
Tips
- Use
\qin psql (PostgreSQL) as an alternative toquit;orexit;— it’s a common shortcut. - In SQL*Plus (Oracle),
exitworks without a semicolon, but including it (exit;) is harmless and promotes consistency across clients. - If you’re in a multi-line statement (e.g., after typing
SELECT * FROMand pressing Enter without a semicolon), finish the statement with a semicolon first, then typeexit;. - Some clients show a help message when you type
\horhelp; use this to confirm the correct exit command if unsure. - Always exit cleanly to avoid leaving idle connections that could exhaust database resources over time.
Troubleshooting
- Session doesn’t respond after typing
exit;: Press Enter again to ensure the command was submitted. If still stuck, tryCtrl+Cto cancel any ongoing input, then retryexit;. - Seeing an error like “Unknown command”: You may be in a different context (e.g., inside a stored procedure script or SQL file). Ensure you’re at the main SQL prompt, not nested in a script.
- Using sqlcmd (Microsoft SQL Server):
exitworks without a semicolon, butexit;is also accepted. If it fails, check if you’re in :r (remote) mode or have active error handling. - Using sqlite3: Type
.exitor.quit(note the dot prefix). The standardexit;will not work here — this is a common point of confusion. - Still at the SQL prompt after multiple attempts: Close the terminal window or tab as a last resort. Then, check for hanging processes using
ps(Linux/Mac) or Task Manager (Windows) and end any orphaned SQL client processes.
Frequently Asked Questions
What’s the difference between exit and quit in SQL?
In most SQL clients, exit and quit are synonymous and both terminate the session. The semicolon is required in clients like MySQL and PostgreSQL to execute the command, but not in others like SQL*Plus or sqlcmd.
Do I need to commit my transactions before exiting?
It depends on the client and autocommit settings. In MySQL with autocommit enabled, changes are saved automatically. In PostgreSQL or Oracle, uncommitted transactions may be rolled back on exit unless explicitly committed. Always verify your autocommit mode or use COMMIT; before exiting if data persistence is critical.
Can I exit using a keyboard shortcut instead of typing?
Generally, no — pure SQL command-line interfaces rely on text input. Shortcuts like Ctrl+D (EOF) may work in some Unix-based clients (e.g., psql) to exit, but this is not universal. Typing exit; or quit; is the most reliable method.
What happens if I exit during a long-running query?
Exiting while a query is running will typically cancel the query and terminate the session. However, this depends on the client and server configuration. For safety, wait for queries to finish or use Ctrl+C to interrupt them first before exiting.
Is it safe to just close the terminal window?
While it will close the session, it’s not recommended as a first option. Abrupt closure can leave server-side connections in an idle state, potentially leading to resource leaks or timeout issues. Always use the proper exit command when possible.
Why does exit; not work in sqlite3?
The sqlite3 client uses dot-commands, not SQL statements, for control functions. You must type .exit or .quit (with a leading dot) to terminate the session. Using exit; here is interpreted as invalid SQL and will produce an error.