FlowsBook a callSign inCreate free accountSign up
AI employees and automation

An AI agent that writes to a database: new places and leads into your own tables

How an AI employee saves what it finds into PostgreSQL, BigQuery or Snowflake every run: write permission, matching columns, adding or updating rows, keeping each once.

Part of the guide AI employees

By the xMap team · · 5 min read

An AI agent that writes to a database saves what it finds straight into a table your company already uses, so the data team does not need a separate import. In xMap Flows this is the Save to my database block: each run, the places, leads, job postings or road closures an AI employee found go into a table in your own PostgreSQL, BigQuery or Snowflake, one row per item, with each field written to the column of the same name.

This article is for data and operations teams who want location results next to their sales, store and customer tables, and for the people who approve what an outside tool may write. It covers how to set up the permission safely, how fields become columns, the two ways of writing, how to avoid duplicate rows, and a worked example.

Why write into your own tables

Most teams already have their dashboards, reports and models pointed at one warehouse. A list in an email or a spreadsheet then has to be copied in by someone, and it usually stops being copied after a few weeks. When the AI employee writes the rows itself, the new places found this morning are in the same tables as last year's sales by lunchtime, joined by the same keys, and every dashboard that reads that table picks them up without anyone touching it.

Who allows the writing

Writing is off by default. A database connection on the Connections page is for reading until whoever manages it sets Save to my database to Allowed. The database sign-in behind it also needs the right to add rows to the table, and to change rows if you plan to update by a key. Three rules keep this safe:

  • The table must already exist. The block never creates, changes or drops a table. Your data team decides the table, its columns and its types.
  • Use a sign-in with narrow rights. Give the account insert, and update where needed, on the one table or schema the flow writes to, nothing more.
  • Real runs only. A test run never writes, so you can check that the items carry the fields your columns expect before the first real run.

If the connection is not set to Allowed, the editor marks the block before you run anything, and Run once and Deploy stop with a message saying why.

How fields become columns

Every item carries fields: a place has name, category, address, city, rating, website, phone, lat, lng and an id; a job posting has company, job title, location and when it was posted. The block writes each field into the column with the same name and leaves out fields that have no column. So a table with name, city, rating and place_id columns gets exactly those four, whatever else the item carried. If you want a field under another name, add a Calculated field before the block, such as store_rating = rating, and make the column match.

Add new rows, or add and update

  • Add new rows: each item becomes a row. Put Only new before it so a place the flow found yesterday is not added a second time today.
  • Add or update by a key: a row whose key, such as place_id, is already in the table is updated, and a new key is added. Ratings and phone numbers then stay current. PostgreSQL needs a unique index or primary key on that column; Snowflake does it with a merge; BigQuery only adds rows, so on BigQuery use Only new and keep the latest row in a view.

A worked example

The company and table here are made up. An analytics team at a food distributor in Johannesburg, Cape Town and Durban wants every new restaurant and cafe in its own PostgreSQL table, refreshed each morning, so its sales dashboard can show new outlets per territory. The table is called outlets_new, with columns poi_id (primary key), name, category, city, address, phone, website, rating, lat, lng and first_seen.

  1. Schedule: every morning at 05:30.
  2. Area of interest: Johannesburg, Cape Town and Durban.
  3. Nearby places: restaurants and cafes in each city, opened in the last 12 months.
  4. Only new: never seen before, matched by the place's own id.
  5. xMap LLM: a fit score from 0 to 100 for the distributor's range, saved as fit_score, with a column added for it.
  6. Save to my database: add or update public.outlets_new by poi_id, the place's own id in xMap's places database.
  7. Email to the analytics lead: how many rows were added this morning.

What lands in the inbox is a short line, while the real result is in the database: new rows in outlets_new that the dashboard reads at 07:00, each with its score. The items also go on to the next block unchanged, so the same flow can post the strongest ones to a sales channel.

Reading back, and databases inside your network

Writing pairs well with reading. Database query brings rows from your own tables into a flow, for example your stores to search around, and Database lookup checks each item against a table, for example to drop places that are already customers. If the PostgreSQL database is not reachable from the internet, a small runner inside your network can carry the queries and the inserts out to it, so nothing is opened up.

Saving results is one of the quieter jobs of AI employees, and one of the most used: it turns a daily email into a growing dataset. The template New openings into my database sets up most of the example above.

Questions

Which databases can it write to?

PostgreSQL, BigQuery and Snowflake, through a connection on the Connections page that has Save to my database set to Allowed.

Can it create the table for me?

No. The table must exist first. The block only adds rows, or adds and updates them by a key column, and never changes the table itself.

What happens to fields that have no column?

They are left out of the row. They stay on the item, so later blocks such as an email still see them.

Does a test run write anything?

No. Only real runs write. Use a test run to see the fields each item carries and compare them with your columns.

Templates that do this

Read next

More on ai employees and automation · Every article · Templates

Put it to work

An AI employee in xMap Flows does this on live data, on its own timetable. Start from a template or describe it in plain words.