> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fly.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Query Database

> Run read-only SQL against a database and return the final result set. The SQL runs as pg_read_all_data inside a read-only transaction with a statement timeout, so it can read every table but cannot change data or schema. Results over 1000 rows are rejected rather than truncated; add a LIMIT. System databases cannot be queried. Requires only read access to the organization.



## OpenAPI

````yaml /api/machines/openapi.json post /v1/postgres/{postgres_cluster_id}/databases/{database_name}/query
openapi: 3.1.0
info:
  title: Machines API
  description: >-
    This site hosts documentation generated from the Fly.io Machines API OpenAPI
    specification. Visit our complete [Machines API
    docs](https://fly.io/docs/machines/api/) for how to get started, more
    information about each endpoint, parameter descriptions, and examples.
  contact: {}
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: '1.0'
servers:
  - url: https://api.machines.dev
security:
  - FlyBearerAuth: []
tags:
  - name: Apps
    description: >-
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/apps-resource/) for details about
      using the Apps resource.
  - name: Machines
    description: >-
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/machines-resource/) for details
      about using the Machines resource.
  - name: TLS Certificates
    description: >
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/certificates-resource/) for details
      about using the TLS Certificates resource.
  - name: Volumes
    description: >-
      This site hosts documentation generated from the Fly.io Machines API
      OpenAPI specification. Visit our complete [Machines API
      docs](https://fly.io/docs/machines/api/volumes-resource/) for details
      about using the Volumes resource.
  - name: Postgres Clusters
    description: >-
      Create and manage Postgres clusters, including databases, users,
      extensions, backups, and app attachments.
externalDocs:
  url: https://fly.io/docs/machines/working-with-machines/
paths:
  /v1/postgres/{postgres_cluster_id}/databases/{database_name}/query:
    post:
      tags:
        - Postgres Clusters
      summary: Query Database
      description: >-
        Run read-only SQL against a database and return the final result set.
        The SQL runs as pg_read_all_data inside a read-only transaction with a
        statement timeout, so it can read every table but cannot change data or
        schema. Results over 1000 rows are rejected rather than truncated; add a
        LIMIT. System databases cannot be queried. Requires only read access to
        the organization.
      operationId: Postgres_databases_query
      parameters:
        - name: postgres_cluster_id
          in: path
          description: Managed Postgres Cluster ID
          required: true
          schema:
            type: string
        - name: database_name
          in: path
          description: Database Name
          required: true
          schema:
            type: string
      requestBody:
        description: Query
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/queryPostgresDatabaseRequest'
        required: true
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/postgresQueryResponse'
        '400':
          description: sql is missing, not a string, or too long
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PostgresErrorResponse'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PostgresErrorResponse'
        '404':
          description: >-
            'Cluster not found' for the cluster; 'Database not found' for the
            database
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PostgresErrorResponse'
        '422':
          description: >-
            The query ran and failed; status is one of invalid_request,
            permission_denied, read_only_violation, statement_timeout,
            row_limit_exceeded, execution_failed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PostgresErrorResponse'
        '500':
          description: Unable to run the query
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PostgresErrorResponse'
        '503':
          description: >-
            pg-admin is unavailable; retry after the interval given by the
            Retry-After header
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PostgresErrorResponse'
components:
  schemas:
    queryPostgresDatabaseRequest:
      required:
        - sql
      type: object
      properties:
        sql:
          type: string
          description: >-
            SQL to run, up to 102400 bytes. Multiple statements are allowed;
            only the last result set is returned.
          example: SELECT id, email FROM users LIMIT 10
    postgresQueryResponse:
      type: object
      properties:
        columns:
          type: array
          description: Columns of the final result set, in order.
          items:
            $ref: '#/components/schemas/PostgresQueryColumn'
        row_count:
          type: integer
          description: Number of rows returned.
        rows:
          type: array
          description: >-
            Rows of the final result set; each row holds one JSON value per
            column, with NULL as null and json/jsonb as decoded JSON.
          items:
            type: array
            items:
              type: object
    PostgresErrorResponse:
      type: object
      properties:
        error:
          type: string
          description: Human-readable error message.
        status:
          type: string
          description: Optional machine-readable error status.
          example: sessions_not_terminated
    PostgresQueryColumn:
      type: object
      properties:
        name:
          type: string
          description: Column name as returned by Postgres.
        type:
          type: string
          description: Postgres type name.
          example: int8
  securitySchemes:
    FlyBearerAuth:
      type: http
      scheme: bearer
      description: >-
        A Fly API token with access to the requested organization or resource.
        Send it as `Authorization: Bearer <Fly API token>`.

````