To run a SQL command-line client, open your terminal (macOS/Linux) or Command Prompt/PowerShell (Windows) and start the client for your database: for MySQL use mysql -u your_user -p, for PostgreSQL use psql -U your_user -d your_database, for SQLite use sqlite3 your_file.db, and for SQL Server use sqlcmd -S server_name -U your_user. You need the client installed and a database (or file) to connect to. The steps below use MySQL and PostgreSQL as the main examples because they are the most common, but the same pattern applies to any client.

What You'll Need

  • A terminal or command prompt. On Windows, Command Prompt or PowerShell both work; on macOS and Linux, Terminal is built in.
  • The database server running, either locally or on a remote host you can reach.
  • The command-line client installed. On many systems it comes with the server package; otherwise install it from your OS package manager or the official database download page.
  • Connection details: host name (often localhost), port (MySQL default 3306, PostgreSQL default 5432, SQL Server default 1433), username, password, and database name.
  • For SQLite, only the sqlite3 binary and a database file path are needed; there is no server or password.

Menu and flag names can differ slightly by client version, operating system and region, so if a flag below is rejected, run client_name --help to see the exact options your build supports.

Step-by-Step

  1. Open your terminal. On Windows press Start, type cmd or PowerShell, and open the result. On macOS open Terminal from Applications > Utilities. On Linux open your terminal emulator.
  2. Check the client is installed. Run mysql --version, psql --version, sqlite3 --version or sqlcmd -? depending on your database. A version line means you are ready; a "command not found" message means you need to install the client or add it to your PATH.
  3. Connect to the server. Use the command for your database:
    • MySQL: mysql -h localhost -P 3306 -u your_user -p. Press Enter, then type the password when prompted. The password is not echoed as you type, which is normal.
    • PostgreSQL: psql -h localhost -p 5432 -U your_user -d your_database. You will be asked for the password unless your system is configured for peer or trust authentication.
    • SQLite: sqlite3 /path/to/your_file.db. If the file does not exist, SQLite creates an empty database at that path.
    • SQL Server: sqlcmd -S server_name -U your_user -P your_password. On Windows you can also use -E for Windows authentication.
  4. Confirm you are connected. In MySQL run SELECT VERSION(); or SHOW DATABASES;. In PostgreSQL run SELECT version(); or \l to list databases. In SQLite run .databases. A result table means the connection works.
  5. Select the database you want to work in. MySQL: USE your_database;. PostgreSQL: \c your_database. SQLite: open the file you want, or use .open /path/to/file.db. SQL Server: USE your_database;.
  6. Run a query. Type a statement ending with a semicolon, for example SELECT * FROM your_table LIMIT 10;, and press Enter. The client prints the rows or a confirmation message.
  7. Exit cleanly. Type exit or quit in MySQL, \q in PostgreSQL, .quit in SQLite, and exit or quit in sqlcmd. Closing the window without exiting can leave the session open on the server until it times out.

Common Mistakes

  • Forgetting the semicolon. Most clients wait for a terminating ; before running the statement. If the prompt changes to -> or ..., you are still in the middle of a statement; type ; and press Enter, or use the client's cancel key (often Ctrl+C) to start over.
  • Putting the password on the command line. Passing -p your_password leaves the password in your shell history and process list. Use the prompt instead: -p with no value.
  • Wrong port or host. A connection refused error usually means the server is not running, the port is wrong, or a firewall is blocking it. Check the service is up and the port matches the server configuration.
  • Running administrative commands without the right user. Creating databases or granting privileges requires an account with those rights; a normal user will get a permission error.
  • Deleting data without a WHERE clause. DELETE FROM table; removes every row. Run a SELECT with the same condition first and use a transaction where the client supports it.

Tips

  • Use --help on any client to see connection flags for your exact version.
  • Most clients keep a history file, so you can press the up arrow to recall the last commands. Avoid typing secrets on the command line if history is shared.
  • For repeatable work, put statements in a .sql file and feed it to the client, for example mysql -u user -p database < script.sql or psql -U user -d database -f script.sql.
  • Use \? in PostgreSQL and help or \h in MySQL to list client commands and SQL help.
  • Wrap destructive changes in a transaction where supported: BEGIN; ... COMMIT; or ROLLBACK;.

Troubleshooting

If the client command is not found, install the client package or add its bin directory to your PATH, then open a new terminal so the change takes effect. If you get "access denied" or "password authentication failed", re-enter the password carefully, confirm the username and host, and check the server's authentication settings; a password reset should be done by an administrator. If the connection times out, confirm the server is running, the host and port are correct, and any firewall or VPN allows the connection. If you see "unknown database", list databases with SHOW DATABASES; or \l and connect to one that exists, or create it with the right privileges. If a query hangs, cancel it with Ctrl+C and check for locks or a missing index. If output is garbled, set the client character set to match the server, for example --default-character-set=utf8mb4 in MySQL. As a last resort, close the session and reconnect; do not drop or recreate databases to fix a connection problem, because that deletes data.

Frequently Asked Questions

Do I need the server installed to use the command-line client?

No. The client can connect to a server on another machine. You only need the client binary and valid connection details. SQLite is the exception in the other direction: it is a file-based database with no server at all.

How do I run a SQL file instead of typing commands?

Redirect the file into the client, for example mysql -u user -p database < file.sql, or use the client's file option such as psql -f file.sql or SQLite's .read file.sql.

Why does my prompt show a dash or dots instead of running my query?

The client is waiting for the statement to end. Add a semicolon and press Enter, or cancel with Ctrl+C and retype the statement.

Can I use the command-line client on a remote server over SSH?

Yes. Connect with SSH first, then run the client on that machine. This is often simpler than opening the database port to the internet, which you should avoid unless it is secured.

How do I stop a long-running query?

Press Ctrl+C in the client to cancel it. If the client does not respond, open a second session with sufficient privileges and cancel the query from there.

Is it safe to put my password in a script?

It is better to use a protected option file or environment variable that the client reads, rather than hard-coding the password. Restrict file permissions so other users cannot read it.