Editing data in a PostgreSQL table

Screenshot table

A table buffer shows some metainformation on the table and its columns, then the row data in tabular form.

If the table contains a large number of rows, the contents will be paginated, with Next and Previous buttons to move page by page. The number of rows in each page is determined by the variable pgmacs-row-limit.

The following keys are bound when the point is located in the table:

KeyBinding
vDisplay the value at point in a dedicated buffer.
RETEdit the value at point in the minibuffer.
wEdit the value at point in a widget-based buffer.
DELDelete the row at point.
M-leftMove to the previous column.
M-rightMove to the next column.
+Insert a new row into the current table, prompting for new values in the minibuffer.
iInsert a new row, prompting for new values in a dedicated buffer.
kCopy the current row.
yPaste (yank) the copied row.
jCopy the current row to the kill ring in JSON format.
<Move point to the first row of data.
>Move point to the last row of data.
numberMove point to the nth column (numbering is zero-based).
eOpen a new buffer to display the result of an SQL query.
rRedraw the table (does not refetch data from PostgreSQL).
nNext page of output (if table contents are paginated).
pPrevious page of output (if table contents are paginated).
SSort the table by the current column.
{Make the current column narrower.
}Make the current column wider.
qKill the current buffer.

Editing a column value

If your table has a primary key, you can edit the contents of the table. To modify a value, move the cursor to the relevant column value and type RET. This will prompt you for the new value, and update the row to the value you specified (it sends PostgreSQL an SQL command similar to UPDATE table_name SET column_name to X WHERE pk_col1 = value1 AND pk_col2 = value2, where pk_col1 and pk_col2 are the names of the columns that comprise the primary key constraint).

Note that PGmacs tells you the column type when prompting for the new value. You must specify a value in the format accepted by PostgreSQL for that type (check the current value as displayed in the minibuffer if you’re unsure of the exepcted format).

For certain column types and very long column values, it may be more convenient to use the widget-based editing interface to edit a column value. Move the cursor to the relevant column value and type w. This will open a dedicated buffer with an editing widget suitable for that column’s type, as illustrated below for an HSTORE key->value map.

Screenshot of hstore editing widget

If you wish to abort editing, simply kill this editing buffer.

Inserting, copying and deleting rows

To insert a new row into a table, press + in the table buffer. You will be prompted for the values of each column for which a default value is not specified (in the minibuffer), then the new row will be inserted. You can also insert a new row by entering new values in a widget-based buffer by pressing i (this may be more convenient if the table contains many rows, or the values to enter are very long).

To delete the row at point, press <delete> or <backspace> in a table buffer and confirm. Please note that this deletes the current row in the PostgreSQL database, as well as in the Emacs buffer. Deleting rows is only possible on tables that include a primary key constraint (this can be a single column specified as PRIMARY KEY, or a constraint across a group of columns).

To copy/paste rows, press k to copy the row to the PGmacs kill buffer (this only copies, without deleting the row), then y to insert a new row with the same values. Any columns that have a default value specified (for example, primary key rows that pull a value from an integer sequence, or are specified as SERIAL, or timestap values that default to now) will be inserted with a new generated value, rather than the value in the copied row.

All updates, insertions and deletions are immediately made on the PostgreSQL server by sending it the appropriate SQL UPDATE TABLE, DELETE FROM or INSERT INTO commands.

Viewing output from an SQL query

You can also view (but obviously not edit!) the output from an SQL query you enter. Type e in a PGmacs buffer, which will prompt you for an SQL query, then display the output in a dedicated temporary buffer. Type q to kill the temporary buffer.

Screenshot table