pgWrite

pgWrite#

pgWrite(conn, tableName, data, varargin)#

Write a MATLAB table to a PostgreSQL database table, adding columns if needed.

Enhanced version of MATLAB’s sqlwrite that handles vector-valued columns and automatically adds missing columns to the database table with appropriate array types (numeric[], int[], text[]).

Parameters:
  • conn (database.postgre.connection) – Open PostgreSQL database connection

  • tableName (string) – Target database table name

  • data (table) – MATLAB table to write to database

  • isForceArray (logical, optional) – Force new columns to be created as array types (default: false)

Notes:

  • Extra variables in data that do not exist in the DB table are automatically added.

  • Vector-valued cells are mapped to PostgreSQL array column types (numeric[], int[], text[]).

  • Column names are case-sensitive and quoted for PostgreSQL compatibility.

Example:

conn = createWriter("myDatabase");
data = table([1; 2], ["a"; "b"], VariableNames=["id", "name"]);
pgWrite(conn, "myTable", data);