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

  1. 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.

  2. Create or Open a Database:
    1. To create a new SQLite database, run: touch mydatabase.sqlite This creates a file in your current directory.
    2. To open an existing database, use: sqlite3 mydatabase.sqlite Replace "mydatabase.sqlite" with your file name.
  3. Run SQL Commands:
    1. 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.
    2. To create a table, use: .tables to list tables and verify structure. Example: CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT, email TEXT UNIQUE );
    3. Use INSERT INTO table_name VALUES(...); to add data. Example: INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
  4. Exit SQLite:
    1. Type .exit when done, or press Cmd+D.

If That Did Not Work

Run this first:
  1. Check if SQLite is installed: sqlite3 --version If missing, install via brew install sqlite.
  2. 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 -- help inside SQLite for command lists.

Tips

  • Use --init-file=file.txt to 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) or mysql (MySQL) after installing them separately.
  • Log SQL output with SELECT * FROM table INTO OUTFILE 'file.csv' to save results.

Troubleshooting

  1. 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/directory sqlite3 mydatabase.sqlite
  2. Connection refused:
    • If using a remote database (e.g., MySQL), verify:
      1. The server is running.
      2. The port (default: 3306 for MySQL).
      3. Credentials are correct (use mysql -u username -p to test).
    • For SQLite, remote access isn’t possible—it’s file-based only.
  3. SQLite shell won’t start:
    • Run sqlite3 without a file to test: sqlite3 If it works, locate your database file manually.

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.