Running a SQL command line application lets you interact directly with a database using text-based commands instead of a graphical interface. This gives you precise control over queries, batch operations, and scripting—essential for database administrators, developers, and power users. Before you begin, ensure you have a SQL database server installed and running (like PostgreSQL, MySQL, or SQLite), and the corresponding command line client tool for that database. This guide assumes you're using a local installation and have basic terminal or command prompt access.
What You'll Need
- A compatible SQL database server installed and running (e.g., MySQL, PostgreSQL, SQLite, SQL Server)
- The corresponding command line client tool for your database (e.g.,
mysql,psql,sqlite3) - Access to a terminal (macOS/Linux) or Command Prompt/PowerShell (Windows)
- Database credentials: username, password, database name, and host address (usually
localhostfor local setups) - Network connectivity if connecting to a remote server
Step-by-Step
- Open your terminal or command prompt. On macOS or Linux, use Terminal. On Windows, search for "Command Prompt" or "PowerShell" and open it as an administrator if required by your database setup.
- Verify the SQL client tool is installed and accessible. Type the client’s command (e.g.,
mysql --version,psql --version, orsqlite3 --version) and press Enter. If you see a version number, the tool is ready. If you get "command not found", the tool isn’t installed or not in your system PATH—install it via your package manager or database installer. - Connect to your database using the correct command syntax. For MySQL, use:
mysql -u username -p database_name. For PostgreSQL:psql -U username -d database_name -h localhost. For SQLite:sqlite3 database_file.db. Replaceusername,database_name, anddatabase_file.dbwith your actual values. The host flag (-h) is only needed for remote connections; for local, it’s often optional. - When prompted, enter your password. Note: you will not see characters as you type—this is normal. Press Enter after typing the password. If authentication fails, double-check your username, password, and database name.
- Once connected, you’ll see a prompt specific to your SQL client (e.g.,
mysql>,postgres=>, orsqlite>). This indicates you’re in the SQL command line interface. - Type a simple SQL command to test the connection, such as
SHOW DATABASES;(MySQL),\l(PostgreSQL), or.databases(SQLite), then press Enter. If you see a list of databases or tables, the connection is successful. - Run your intended SQL query. For example:
SELECT * FROM users;(ensure table names and syntax match your schema). End every command with a semicolon (;) unless the client accepts otherwise. - To exit the SQL client, type
EXIT;or\q(PostgreSQL), then press Enter. Do not close the terminal window directly—this may leave connections open on the server.
Common Mistakes
- Forgetting the semicolon at the end of SQL commands, which causes the client to wait for more input
- Typing the wrong database name or username—case sensitivity matters on some systems
- Running the client command without starting the database server first (e.g., MySQL service not running)
- Using the wrong client tool for your database (e.g., trying
psqlto connect to a MySQL server) - Confusing SQLite file paths—SQLite requires the full path to the .db file, not just the name
- Entering the password on the command line (e.g.,
-p mypassword), which can be logged in shell history and is a security risk
Tips
- Use environment variables to store credentials securely (e.g.,
MYSQL_PWDfor MySQL, though this is discouraged for shared systems) - Save frequently used queries in text files and use
source filename.sqlin the client to execute them - Use
\hin MySQL or\?in PostgreSQL to see help commands available in the client - Enable command history: most clients support arrow keys to recall previous queries
- For remote connections, ensure the server allows external connections and firewall rules permit traffic on the SQL port (e.g., 3306 for MySQL, 5432 for PostgreSQL)
- Always test queries on a copy of your data first—command line tools execute commands immediately with no undo
- Use
\timingin PostgreSQL or\tin MySQL to measure query execution time for optimization
Troubleshooting
- "Command not found": Install the correct client tool. On Ubuntu/Debian, use
sudo apt install mysql-clientorsudo apt install postgresql-client. On macOS, use Homebrew:brew install mysql. On Windows, download the official MySQL or PostgreSQL installer and ensure "Add to PATH" was selected during installation. - "Access denied": Verify your username and password. Check if your user account has permissions to access the database. For MySQL, you may need to grant access with
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';as root. - "Can't connect to local MySQL server": Start the MySQL service. On Linux:
sudo systemctl start mysql. On Windows: open Services, find MySQL, and click Start. Repeat for PostgreSQL usingpostgresqlorpg_ctl. - "Unknown database": List existing databases first with
SHOW DATABASES;(MySQL) or\l(PostgreSQL). Create the database if needed:CREATE DATABASE mydb;. - SQLite file not found: Use the full absolute path (e.g.,
/home/user/data/mydb.dbon Linux, orC:\Users\user\data\mydb.dbon Windows). Relative paths may not resolve correctly depending on your current directory. - Connection timeout on remote server: Confirm the server’s IP and port are correct. Check if the server’s firewall allows inbound connections. Test connectivity with
pingortelnet server_ip port.
Frequently Asked Questions
Can I run SQL commands without installing a database server?
No. The command line tool is just a client—it requires a running database server to connect to. SQLite is the exception: it doesn’t need a server, as it operates directly on a file.Is it safe to enter passwords in the command line?
It’s discouraged. If you type the password as part of the command (e.g.,-pmypassword), it may be saved in your shell history or process list, making it visible to other users. Always use the interactive prompt (just -p) to enter passwords securely.
Why does my SQL command hang after I press Enter?
You may have forgotten a semicolon. The client waits for the end of the statement. Type; and press Enter. If that doesn’t work, check for unmatched quotes or parentheses in your query.
Can I run SQL scripts from a file?
Yes. Use thesource command in MySQL (source /path/to/query.sql) or \i /path/to/query.sql in PostgreSQL. This is useful for running complex or repeated queries.
What if I forget my database password?
You’ll need to reset it using administrative access. For MySQL, restart the server with--skip-grant-tables, then update the password manually. For PostgreSQL, edit the pg_hba.conf file to allow trust authentication temporarily. Always back up your data before doing this.