Yes, you can run SQL commands directly in your macOS Terminal—no need for separate databases or GUI tools. This method works for SQLite databases, which are built into macOS, or for running simple SQL queries against remote servers if you have the connection details. Here’s how to do it efficiently and safely.
What You'll Need
- Mac running macOS Catalina (10.15) or later
- An installed database (SQLite is pre-installed; others like MySQL or PostgreSQL require separate software)
- Basic familiarity with the Terminal (Command Prompt) to navigate files and run commands
- If using remote databases, the connection details (host, port, credentials)
Step-by-Step
-
Choose a Database: macOS comes with SQLite preinstalled. To use it, launch Terminal (press Cmd+Space, type Terminal, and hit Enter).
If you need another database (MySQL, PostgreSQL), install it separately via Homebrew or the official installer. For this guide, we’ll use SQLite, which doesn’t require setup beyond opening Terminal.
-
Create or Open a Database:
- To create a new SQLite database, run:
touch mydatabase.sqliteThis creates a file in your current directory. - To open an existing database, use:
sqlite3 mydatabase.sqliteReplace "mydatabase.sqlite" with your file name.
- To create a new SQLite database, run:
-
Run SQL Commands:
- Once inside the SQLite shell (you’ll see the "sqlite> " prompt), type commands directly:
SELECT * FROM table_name;Example:SELECT * FROM users;to query a table. - To create a table, use:
.tablesto list tables and verify structure. Example:CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT, email TEXT UNIQUE ); - Use
INSERT INTO table_name VALUES(...);to add data. Example:INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
- Once inside the SQLite shell (you’ll see the "sqlite> " prompt), type commands directly:
-
Exit SQLite:
- Type
.exitwhen done, or press Cmd+D.
- Type
If That Did Not Work
Run this first:- Check if SQLite is installed:
sqlite3 --versionIf missing, install viabrew install sqlite. - Verify the database file exists in your current directory:
ls
Common Mistakes
- Permissions errors: SQLite works with the current directory by default. If the file isn’t accessible, move the SQLite file into your home folder (e.g., ~/Documents) or use absolute paths like
/Users/yourname/mydatabase.sqlite. - Missing tables: Ensure tables exist before querying. Create them first:
CREATE TABLE table_name (...); - Syntax errors: SQLite is case-insensitive for keywords but requires exact syntax. Use
-- helpinside SQLite for command lists.
Tips
- Use
--init-file=file.txtto save queries to a file for later use. - Avoid spaces in table/column names by wrapping them in backticks:
`my_table`. - For remote databases, use tools like
psql(PostgreSQL) ormysql(MySQL) after installing them separately. - Log SQL output with
SELECT * FROM table INTO OUTFILE 'file.csv'to save results.
Troubleshooting
- Command not found:
- Reinstall SQLite via Homebrew:
brew uninstall sqlite && brew install sqlite - Ensure the database file is in your current directory:
cd /path/to/directorysqlite3 mydatabase.sqlite
- Reinstall SQLite via Homebrew:
- Connection refused:
- If using a remote database (e.g., MySQL), verify:
- The server is running.
- The port (default: 3306 for MySQL).
- Credentials are correct (use
mysql -u username -pto test).
- For SQLite, remote access isn’t possible—it’s file-based only.
- If using a remote database (e.g., MySQL), verify:
- SQLite shell won’t start:
- Run
sqlite3without a file to test:sqlite3If it works, locate your database file manually.
- Run
Frequently Asked Questions
Can I use this for MySQL or PostgreSQL?
Yes, but you need to install drivers separately. For MySQL, install via Homebrew:
brew install mysql
Then connect with:
mysql -u username -p
For PostgreSQL (macOS includes it by default):
psql -U username
What if I don’t have SQLite installed?
Install it with:brew install sqlite
It’s lightweight and works for small projects.
How do I save my SQL results?
Use:SELECT * FROM table INTO OUTFILE 'output.csv'
Then open output.csv in a text editor.
Why can’t I query a remote database from Terminal?
macOS Terminal lacks direct remote database support for SQLite. For remote databases, use:mysql (MySQL) or psql (PostgreSQL), which have remote connection features.
Is this safe to use for production queries?
Yes, for local SQLite databases. For remote databases, ensure credentials are secure and avoid hardcoding them in scripts. Use environment variables for credentials in production.How do I reset a corrupted SQLite database?
Delete the file and recreate it:rm mydatabase.sqlite
touch mydatabase.sqlite
sqlite3 mydatabase.sqlite
---
This method is efficient for SQLite and provides a foundation for learning SQL in the command line. For remote databases, pair Terminal with dedicated client tools.