pgWrite#
- pgWrite(conn, tableName, data, varargin)#
Write a MATLAB table to a PostgreSQL database table, adding columns if needed.
Enhanced version of MATLAB’s
sqlwritethat 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 connectiontableName (
string) – Target database table namedata (
table) – MATLAB table to write to databaseisForceArray (
logical, optional) – Force new columns to be created as array types (default: false)
Notes:
Extra variables in
datathat 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);