Adding a new column is routine, but small mistakes turn routine into downtime. In relational databases, defining schema changes with precision is critical. A new column changes table structure, affects existing queries, and can alter performance. You need to plan the data type, default value, indexing, and constraints before executing.
In PostgreSQL, the core command is simple:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP DEFAULT NOW();
This adds the column with a default timestamp, applying it to existing and future rows. In production, though, you must consider lock timing. On large datasets, adding a column with a non-null default rewrites the entire table, which can stall traffic. For MySQL, a similar command exists:
ALTER TABLE users ADD COLUMN last_login DATETIME DEFAULT CURRENT_TIMESTAMP;
Here, engine differences matter. MySQL may lock the table during the operation depending on the storage engine and version. For high-traffic apps, use an online schema change process with tools like gh-ost or pt-online-schema-change.