Skip to content

Generic SQL Connection

The Generic SQL connection allows you to integrate any PostgreSQL or MySQL database with OpsTower using a standard connection string. Agents can run read-only SQL queries against your database for data lookups and analysis. All queries are limited to SELECT statements and return a maximum of 100 rows per query.

You need a PostgreSQL or MySQL database that is accessible over the network. The database must accept connections from external hosts (or be reachable through a tunnel).

To set up a Generic SQL connection, you need two pieces of information:

  • Connection String — a standard database connection URI
  • Database Type — either PostgreSQL or MySQL
postgres://user:password@host:5432/dbname

or

postgresql://user:password@host:5432/dbname
mysql://user:password@host:3306/dbname

Replace user, password, host, 5432/3306, and dbname with your actual database credentials, host address, port, and database name.

OpsTower validates all queries as read-only before execution, but it is strongly recommended to create a dedicated read-only database user for the connection. This provides defense in depth.

CREATE USER opstower WITH PASSWORD 'your-secure-password';
GRANT CONNECT ON DATABASE your_database TO opstower;
GRANT USAGE ON SCHEMA public TO opstower;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO opstower;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO opstower;
CREATE USER 'opstower'@'%' IDENTIFIED BY 'your-secure-password';
GRANT SELECT ON your_database.* TO 'opstower'@'%';
FLUSH PRIVILEGES;

Ensure your database is accessible from the internet. Common approaches include:

  • Cloud-hosted databases typically provide a public endpoint that you can enable in your provider’s settings.
  • SSH tunnels or VPN connections may be needed for databases on private networks.
  • Firewall rules should allow inbound connections on the database port from OpsTower’s infrastructure.
  1. In OpsTower, navigate to Connections in the sidebar.
  2. Click Add Connection and select Generic SQL.
  3. Select your Database Type — either PostgreSQL or MySQL.
  4. Enter your Connection String.
  5. Click Save to create the connection.

Once connected and enabled on an agent, the agent can:

  • Execute read-only SQL queries (SELECT statements only) against your database
  • List all tables and schemas in the database
  • Inspect table structures to understand your data model

All queries are validated as read-only before execution. Write operations (INSERT, UPDATE, DELETE, DROP, etc.) are rejected.

  • Connection refused: Verify that the host and port in your connection string are correct and that the database is accepting remote connections.
  • Authentication failed: Double-check the username and password in your connection string. Ensure the user exists and has the correct permissions.
  • SSL errors: Some databases require SSL connections. Check your database provider’s documentation for SSL connection string parameters (e.g., ?sslmode=require for PostgreSQL).
  • Timeout errors: Ensure your database is reachable from the internet. If the database is behind a firewall, configure access rules to allow inbound connections.
  • Permission denied on tables: Verify that the database user has SELECT privileges on the tables you want to query.