Cover image for luis

Luis Ibarra

Technical Support Engineer

Appsmith

Automatically Refresh Postgres Data in Appsmith (With a Modal)

When another service or script updates your Postgres database, you may want your Appsmith app to refresh automatically so users always see the latest results.

Below is a simple and reliable way to do this.

✅ Recommended Approach: Poll a “Last Updated” Value

Appsmith apps cannot directly receive push events from external scripts, so the best solution is:

  1. Your script updates a “sync status” row in Postgres
  2. Appsmith checks that row every few seconds
  3. If new data is detected, Appsmith shows a modal + runs the query again

Step 1 — Add a Sync Status Table in Postgres

Create a small table to track when updates complete:

CREATE TABLE sync_status (
  service TEXT PRIMARY KEY,
  last_run_id TEXT,
  last_completed_at TIMESTAMP DEFAULT now()
);

When your internal script finishes, it updates this row:

UPDATE sync_status
SET last_run_id = 'run_12345',
    last_completed_at = now()
WHERE service = 'my_service';

Step 2 — Create a Query in Appsmith

Create a query called GetSyncStatus:

SELECT last_run_id, last_completed_at
FROM sync_status
WHERE service = 'my_service';

Step 3 — Poll for Changes Using a JS Object

Create a JS Object called SyncWatcher:

export default {
  lastSeen: null,
  timer: null,

  start() {
    this.lastSeen = appsmith.store.lastSeenRunId || null;

    this.timer = setInterval(async () => {
      const res = await GetSyncStatus.run();
      const row = res[0];

      if (!row) return;

      // If new update detected
      if (row.last_run_id !== this.lastSeen) {
        this.lastSeen = row.last_run_id;
        await storeValue("lastSeenRunId", row.last_run_id);

        // Warn user
        showModal("DataUpdatedModal");

        // Refresh your main query
        await YourMainQuery.run();
      }

    }, 15000); // every 15 seconds
  }
};

Step 4 — Start the Watcher on Page Load

On the page’s onPageLoad action, run:

SyncWatcher.start();

Step 5 — Add a Modal for User Warning

Create a modal named:

DataUpdatedModal

Modal message example:

✅ New database updates were detected. The app has refreshed the latest results.

Button action:

closeModal("DataUpdatedModal")

✅ Result

Whenever your script finishes updating Postgres:

  • Appsmith detects the new run
  • A modal warns users
  • Queries refresh automatically
  • Users always see the newest data