Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

SQL CONNECT

The SQL CONNECT command establishes a named connection to an external database.

Syntax

SQL CONNECT <driver> '<dsn>' AS <alias>

Parameters:

ParameterDescription
<driver>Database driver name
<dsn>Data Source Name (connection string)
<alias>Short name to reference this connection

Supported Drivers

DatabaseDriver Names
PostgreSQLpostgres, pg, postgresql
Oracleoracle, ora
SQL Serversqlserver, mssql

Connection Examples

PostgreSQL

SQL CONNECT postgres 'postgres://user:password@localhost:5432/mydb' AS source;

-- With SSL
SQL CONNECT postgres 'postgres://user:password@host:5432/mydb?sslmode=require' AS prod;

Oracle

SQL CONNECT oracle 'oracle://user:password@host:1521/service_name' AS oradb;

SQL Server

SQL CONNECT sqlserver 'sqlserver://user:password@host:1433?database=mydb' AS mssql;

Managing Connections

List Active Connections

SQL CONNECTIONS;

This shows alias and driver name only. The DSN is never displayed for security reasons.

Disconnect

SQL DISCONNECT source;

Multiple Connections

You can maintain multiple simultaneous connections with different aliases:

SQL CONNECT postgres 'postgres://...' AS source_db;
SQL CONNECT oracle 'oracle://...' AS legacy_db;
SQL CONNECT sqlserver 'sqlserver://...' AS reporting_db;

-- Query each by alias
SQL source_db SELECT count(*) FROM users;
SQL legacy_db SELECT count(*) FROM employees;
SQL reporting_db SELECT count(*) FROM reports;

-- List all connections
SQL CONNECTIONS;

-- Disconnect individually
SQL DISCONNECT legacy_db;

CLI Subcommand

For one-off queries without establishing a named connection:

mxcli sql --driver postgres --dsn 'postgres://user:pass@localhost:5432/mydb' "SELECT 1"

Security Notes

  • The DSN (which contains credentials) is never shown in session output or logs
  • Only the alias and driver type are displayed when listing connections
  • See Credential Management for environment variable and YAML config alternatives