Querying CKAN data with SQL through datastore_search_sql

Decide whether to expose datastore_search_sql on a public CKAN portal, and what to configure and test: the SELECT rule, permission checks, row cap, timeout and truncation flag.

On this page

When datastore_search_sql is worth enabling on a CKAN portal

Enable datastore_search_sql only when filtered and paged access cannot answer the questions your users actually ask. The action runs read-only SQL over DataStore tables through the Data API, so it is a query surface for joins, aggregates and expressions that datastore_search cannot express. The DataStore documentation describes the Data API as covering filtering, full-text search, pagination and SQL queries over tables loaded from resources into a separate PostgreSQL database. Start from that list. If filters, full-text search and paging handle the common requests, the SQL endpoint adds exposure without adding much answer value.

Some audiences usually need SQL: analysts who want counts or groupings across a whole table, developers building comparison tools that combine several resources, and researchers who ask questions by category or time period. Application developers building a front end for one table usually manage with filters and pagination, and most casual visitors never need a query at all. Ask each group which questions they cannot answer today and write down the exact queries they want. If that list is short, a documented download or a filtered view may serve them, and the SQL surface stays closed. Confirm the list with your data owners before you assume the demand exists.

A public portal needs a deliberate decision because the permission checks on each resource decide what a query can read, so every dataset's access rules carry into SQL results. Confirm how your CKAN version applies those checks to private and draft datasets before enabling the action, and test with an anonymous account and a collaborator account. Query load is the second concern. One expensive query can slow the database that serves every visitor, so decide who may query, which timeouts and row limits apply, and who reviews the logs. Confirm the current limits for your release and test them on a staging copy first.

Which datastore_search_sql settings to decide before launch

This table compares the six decisions a portal team makes before turning on SQL search for DataStore tables: whether to enable it, who can reach it, how results are capped, whether to publish examples, and when to send users elsewhere. Read each row as a setting, what it controls, when it fits, and what to confirm in the CKAN version you run. The main takeaway is that access and limits should be tested on a live portal with a private resource and a large table before launch.

DecisionWhat it controlsWhen it fitsWhat to confirm
Enable SQL search at allWhether callers can send free-form SELECT statements against DataStore tablesPortals whose data is already public and whose users need direct table queriesConfirm the SELECT-only restriction still applies in your running version
Who can reach itWhich callers can send SQL, with a read permission check on each resource in the queryPublic portals with only public datasets, or internal portals where access follows organization rolesSend a query against a private resource anonymously and as a user without access
Row cap and timeoutHow many rows one SQL response returns and how long a query runs before it stopsShared servers where one heavy query could slow the portal for other visitorsCheck the cap and timeout settings in your version's DataStore docs, then test with a large table
Truncation flagSignals when results were cut at the row cap, so clients know the set is incompleteAny client that counts rows or pages through results and must not treat a capped set as completeConfirm the response includes the records_truncated flag and that your client reads it
Example queriesWhether sample SQL appears to users, and which tables and uppercase column names they exposePortals that want users to learn the schema and have reviewed the column namesQuote uppercase column names in examples and run each example on the live portal
Handoff to datastore_search or downloadsRouting: filtered or paged reads go to datastore_search, full table reads go to the download optionApplication back ends, and users who need complete files instead of a capped queryDocument the handoff on each resource page and confirm the download option is enabled

How CKAN checks a SQL query before it runs

CKAN runs three separate checks on every query sent to datastore_search_sql, and a query must clear all of them. The first check reads the SQL itself: CKAN accepts a single SELECT statement and rejects anything that would change data or chain several statements together. The second check covers access. Each table in the query maps to a DataStore resource, and CKAN confirms that the caller can read every one of those resources before the query runs. A join across two resources therefore needs read access to both, so a caller who can see a public dataset cannot reach a private table through a join. The third check happens during execution, and it depends on server settings and load as much as on the SQL.

The query runs under the read-only database user described in the DataStore docs, a PostgreSQL account separate from the one that loads tables. That account holds read grants only, so the database acts as a second barrier if a statement ever got past the SELECT check. Confirm those grants in your environment with ckan datastore set-permissions and by attempting a write as that user. Failures look different to the caller. A query can pass the syntax check and still fail on access, and the caller gets an error with no rows. A query that runs past the configured timeout also returns an error, so no partial result arrives. A query that returns more rows than the configured row cap succeeds, but the response sets records_truncated to true, and a client that ignores that flag will treat a partial table as complete. Read the timeout and row cap values from your CKAN configuration file, then confirm how your version reports each case.

Before you enable SQL search on a public portal, run four checks with a test account that holds no organization roles: a query against a private resource, a join that spans a public and a private resource, a write statement, and a query over a large table with a broad filter. Record exactly what the caller receives in each case. Decide whether the configured timeout and row cap suit the queries you expect, and state in your API documentation that a result can be truncated. Error wording can change between versions, so build client error handling from your own test output, and repeat these checks after each CKAN upgrade.

Every SQL query passes two checks before any rows are read

A request starts at the client, which calls datastore_search_sql on the CKAN action API with a SQL string. The portal first applies the single SELECT check: anything other than one SELECT statement stops there with an error. The resource-level permission check then confirms the caller can read the table. Only after both checks does the query run under the read-only DataStore role, where a timeout and a row cap limit the work. The response carries the records and a records_truncated flag. When you test, confirm each rejected branch returns an error, and check that flag before treating a result as complete.

Rejected queries stop with an error at two checks before any row is read.

Enabling SQL search and testing it on a staging portal

This sequence turns on datastore_search_sql on a staging copy of the portal and checks each behavior the public endpoint depends on before anything reaches production. Each step produces a recorded result, from the read-only database role through the row cap, permissions, quoting and truncation flag. Example queries are published only after every check passes.

  1. Confirm the DataStore and read-only user

    A CKAN administrator on the staging portal confirms that the DataStore extension is enabled and that the DataStore database has a write user and a separate read-only user, set up as the DataStore documentation describes. The administrator then connects with the read-only credentials and attempts an insert into a test table. The step is done when the read-only role can select data but the insert fails with a permission error.

  2. Record the CKAN version and documented behavior

    The team records the exact CKAN version running on staging and reads the DataStore documentation for that version, because SQL search behavior depends on the release. CKAN patch releases do not break backwards compatibility, but minor releases can include backwards-incompatible changes, so the team notes whether staging runs the latest patch of its minor version. The step closes when the version, patch status and documentation link are written into the change record and match the production portal.

  3. Set the row cap and timeout

    The administrator sets the row cap and the SQL query timeout in the CKAN configuration file on staging, using values agreed with the data owners based on the largest tables and the capacity of the database. Confirm the exact configuration key names in the DataStore documentation for your CKAN version before editing. The administrator then restarts the web processes so the new values load. The step is done when a test query on staging confirms the cap is applied, which the next checks verify.

  4. Test a rejected multi-statement query

    The tester sends a query containing two statements separated by a semicolon, such as a SELECT followed by a DELETE, to datastore_search_sql. The endpoint should reject it because it accepts only a single SELECT statement. The check passes when the response is an error and the row count of the target table is identical before and after the request. The tester saves the error text in the test log so the team can compare it after any future CKAN upgrade.

  5. Test public and private resource permissions

    The tester runs the same simple SELECT against a public resource and a private resource in the same staging portal. Each query runs three ways: anonymously, as a user with no membership in the owning organization, and as a member of that organization. The public query should return rows, the private query should be denied to the first two users, and the member should see the rows. The step is done when each result is logged and no private rows appear in any anonymous or outsider response.

  6. Test uppercase column quoting

    The tester loads a small test table with a column name containing uppercase letters, then runs a query that references that column without quotes. PostgreSQL folds unquoted names to lowercase, so the query should fail because the lowercase name does not exist in the table. The tester then wraps the column name in double quotes and confirms the expected values return. The step is done when both results are recorded and the team has a written quoting rule for publishers.

  7. Confirm the truncated response flag

    The tester queries a table with more matching rows than the configured cap and checks that the response sets records_truncated to true. The tester then runs a query that returns fewer rows than the cap and confirms the flag is false. Client code should treat a true value as an incomplete result set and never as a full total. The step is done when both responses are recorded and the team agrees on how example queries will handle truncation.

  8. Publish example queries after checks pass

    Only after the first seven checks pass does the team publish example queries on the portal, each copied from a test that ran on staging. Each example shows the quoting rule, the single SELECT form and how to read records_truncated. The team links the DataStore documentation for reference. The step is done when the examples are reviewed against the recorded results and the portal owner approves the publication.

Who owns the risk of public SQL access

Security reviewers own the question of whether an anonymous or low-trust caller can reach data they should not see, or touch the database beyond its intended scope. The boundary depends on how the read-only user was created and granted, so review the database grants as well as the CKAN configuration.

  • Read-only user grants: Confirm that the read-only role has SELECT on DataStore tables only, with no write rights and no access to the CKAN application tables or other databases on the same server. A weak grant here can expose data that no resource permission check will catch.
  • Resource-level visibility: Test with an anonymous session and with a logged-in user who lacks access to a private dataset. Confirm that the SQL endpoint applies the same permission checks as the other Data API actions.
  • Scripted abuse: Assume automated clients will send many queries. Ask which request rate, body size and timeout controls sit at the web server or gateway, and whether they cover the SQL endpoint separately.
  • Injection surface: Treat the SQL string as untrusted input. Test crafted statements, including multiple statements and function calls, and confirm the single-SELECT restriction holds in your version.
  • Evidence: Keep the grant output, test results and review date with the approval record.

Recheck the grants and rerun the tests after any CKAN upgrade or database change, using the CKAN DataStore documentation as the reference for the expected setup.

Limits to confirm before you publish SQL examples

The SQL endpoint depends on settings, database permissions and extensions that differ between portals and between CKAN releases. Each card names one item to confirm in your own environment, and the answer should come from your configuration, the DataStore docs or a test on staging before any example goes public.

Row cap and statement timeout

The SQL endpoint returns no more than the row cap configured on your portal, and a query stops when the timeout expires. Read the cap in ckan.ini (the ckan.datastore.search.rows_max setting) and confirm the timeout with your administrator. On staging, run a query that exceeds the cap and check that the response includes the records_truncated flag, so your examples can tell readers when results are incomplete.

DataStore docs

Behavior on your CKAN version

CKAN 2.12.0 was released on August 26, 2026, together with maintenance releases 2.11.6 and 2.10.11. Minor releases may include backwards-incompatible changes, so SQL behavior that worked on the version you first tested can shift after an upgrade. Read the 2.12.0 release notes, then rerun every published example on the exact version your portal runs.

CKAN releases

Read-only database user

The SQL endpoint should connect only through the read-only DataStore user. Confirm the connection URLs in your configuration and the permissions set with the ckan datastore set-permissions command. Then send an INSERT or UPDATE through the API on staging and confirm that PostgreSQL rejects it. Published examples depend on this check, because a SELECT-only rule in the API does not replace database-level permissions.

Uppercase column names

PostgreSQL folds unquoted identifiers to lowercase, so a column loaded with capital letters must be wrapped in double quotes in every example. Load a sample file with mixed-case headers into staging, run each example exactly as written, and compare the column names the loader created. Check the names again after any reload, since an example that worked before a reload can fail after it.

Extensions and custom actions

Extensions and custom actions can wrap, extend or replace the SQL action, its authorization checks and its response shape. List every plugin in ckan.plugins and ask each owner whether it changes datastore actions. Then test each published example with the production plugin set on staging, including requests against private resources, to confirm that resource-level permission checks still apply.

Database load on large tables

A query that scans or joins a large table runs on the same PostgreSQL server that serves the rest of the DataStore. Run your heaviest example against a copy of production-size data while watching query time and server load. Ask your database administrator whether the DataStore database has dedicated resources, and state in the examples which tables are large and which filters keep queries narrow.

Questions teams ask before enabling datastore_search_sql

How much does it cost to enable datastore_search_sql on a CKAN portal?

Cost follows the written scope, and the largest parts are review, testing and handover rather than a single configuration change. The main drivers are how many portal instances and environments are involved, how many datasets and resources need query testing, whether the DataStore database roles and resource permissions are already documented, and whether private datasets need a separate permission review. One-time work covers the configuration review, the test cases and the runbook your team keeps afterward. Recurring cost appears only if you add a retainer for upgrades and continuing tests. In a quote, ask for each deliverable listed separately, the environments covered, the test cases used, who signs off and what is excluded. DeployOpen does not publish prices, so the quote comes from that scope.

How long does it take to enable and test SQL search?

Duration depends on how well the current DataStore setup is documented, how many tables and permission groups need testing, and how long approvals take on your side. No single timeline applies to every portal, so treat any date given before the scope is written as a planning guess. Ask for milestones in the written scope: access confirmed, permission review, query testing in staging, a change window for production and a post-change check. Ask who owns each step on your side. Waiting for data owners and sign-off often sets the pace more than the technical steps do. Confirm the expected duration against your own environment before you book a release date.

What does our portal team need to provide for this work?

The portal team provides access, facts about the current setup and people who can make decisions about data. Typical inputs include:

  • Staging and production access for an administrator who can review configuration, with credentials shared through your approved secrets process
  • The CKAN version, installed extensions and the DataStore database layout, including which roles exist and who manages them
  • A list of organizations and datasets that hold private or sensitive resources, with the owners who must approve access changes
  • A change window for production and a named contact who can approve the change on the day

Ask the written scope to name each input, its owner and the date it is needed. Missing inputs are the most common reason testing stalls.

Can an internal team enable datastore_search_sql without a partner?

An internal team that already runs CKAN and understands the DataStore can do this without a partner, provided it has time for testing and a clear owner for the permission model. The parts that need the most care are confirming that the read-only DataStore role cannot write, checking that resource-level permissions hold for private and draft data, and testing the query patterns your users will actually run. Start with the official DataStore documentation and write down what you verified and how. A partner adds value when your team lacks CKAN or database experience, when deadlines are fixed, or when an independent second review of the permission model is required.

Is SQL search safe on a public CKAN portal?

SQL search can be run safely on a public portal, but only after you verify the controls around it. The main exposure is data a user should not see, such as private resources reached through a query, along with load from expensive queries and information leaked through column names. Verify the single SELECT restriction, the row cap, the query timeout and the resource-level permission checks in your own version. Confirm that the read-only database role cannot write or read tables it should not. Record which datasets are public and which are restricted before testing begins. No vendor setting removes the need for these checks, and any security claim should come from your own test results.

What changes after a CKAN upgrade that affects SQL queries?

After an upgrade, the important change is whether the release is a patch or a minor version. CKAN states that patch releases do not break backwards compatibility and exclude schema changes and new features, while minor releases may include backwards-incompatible changes. CKAN 2.12.0 was released on August 26, 2026, together with maintenance releases 2.11.6 and 2.10.11 on the same day. Treat any minor upgrade as a reason to rerun your SQL query tests and permission checks, and keep patch upgrades on a regular schedule. Check the release policy to see which versions still receive security and bug fixes, because an outdated patch loses support once a newer patch is out.

When should we turn off SQL search on a CKAN portal?

Turn off SQL search when it creates more risk or load than the portal can manage. Common triggers include heavy queries that slow the database for other users, a change to the permission model that has not been tested, private resources added to a dataset without review, an upgrade that has not been verified against your queries, or abuse seen in logs. Before switching it off, tell API users which applications will stop working and plan a date for re-enabling after testing. Confirm how your installed CKAN version disables the endpoint, and test that the change took effect for anonymous and logged-in users. Record the reason and the approver in your change log.

Where SQL search sits next to filtered search and downloads

Start by matching each query type to its caller, because the Data API offers paths that serve different needs. The datastore_search action is the default for application back ends: callers name fields, apply exact and range filters, and page through a bounded set of matching rows. The load it creates is shaped by a few parameters your team can reason about and test. The full-table download endpoint answers another need entirely, a person or tool that wants the whole resource as a file. It is a bulk export, so its cost follows table size and output format, and it should be reviewed as its own access path. Keep filtered search, downloads and SQL listed separately in your design notes so that a change to one does not quietly alter the others.

Stay with filtered search when the consumers are known applications asking fixed questions, when the portal is open to anonymous traffic, or when predictable load matters more than flexibility. Named fields and filters give your team a request shape that can be tested, controlled at an API gateway and documented for integrators, with far fewer ways for a caller to ask an expensive question. Before you publish a filter-driven API on a large table, confirm with your DataStore administrator which columns are indexed, since filter performance depends on it. For many back ends, filtered search answers the question without opening a general query surface at all.

Enable SQL when a question needs joins across resources, aggregation or expressions that filters cannot state, and when the people asking can write and review SELECT statements. SQL justifies its added review because every query passes through the same gate: the single SELECT restriction, resource-level permission checks, and the row cap and timeout must all hold for every caller, including anonymous ones on a public portal. Test each control against your own portal, from an unauthenticated request and with the read-only account, before opening the endpoint. If the need is a fixed summary that a small set of users reads, publishing that summary as a resource is often easier to govern than an open query surface, because the publisher reviews it once and callers never gain a query surface at all.

How DeployOpen scopes and supports DataStore query work

DeployOpen scopes SQL search on CKAN DataStore as a written decision about who can run SELECT queries against which tables, before anything is switched on. We implement it inside your own infrastructure, test the permission and query limits against your portal, and document the result for your operators. Support and upgrades can follow under a retainer.

How we scope

  • Written scope names the portals, resources and tables where datastore_search_sql would be enabled, and those left out.
  • Scope records the permission model: which anonymous, logged-in and organization users can reach each resource.
  • Scope agrees the row cap, query timeout and the client behavior expected when records_truncated is true.

How we build

  • Build the read-only DataStore connection that SQL search depends on, and verify its grants before enabling search.
  • Test that multi-statement, write and non-SELECT input is rejected, using a documented test set for your portal.
  • Test resource-level permission checks with private, organization and anonymous requests, including uppercase column quoting cases.

How we hand over

  • Hand over a written record of the enabled resources, permission rules, and the row cap and timeout values in use.
  • Document how to read records_truncated in responses so client teams do not treat a capped result as complete.
  • Include the test queries and expected results so your operators can rerun the checks after every change.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers, covering SQL search settings after CKAN upgrades.
  • Plan a retest of permission and query limits after CKAN upgrades or DataStore permission changes.
  • Ask for retest and handover items to be named in the written scope before work starts.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.