Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Add a Default Value in PostgreSQL
Adding a default value to a column in PostgreSQL ensures that when no value is provided during an insert, the column automatically gets the default.
Why Use a Default Value?
Default values help keep your data consistent and reduce the need for client-side handling. For example, you might want a status column to default to 'active' or a created_at column to default to the current timestamp.
Basic Syntax
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_value;Example 1: Add a Default String Value
ALTER TABLE users ALTER COLUMN status SET DEFAULT 'active';
This sets the status column of the users table to default to 'active' if no value is provided.
Example 2: Add a Default Numeric Value
ALTER TABLE orders ALTER COLUMN quantity SET DEFAULT 1;Example 3: Add a Default Timestamp
ALTER TABLE posts ALTER COLUMN created_at SET DEFAULT now();Important Tips
- Setting a default does not update existing rows. Use
UPDATEif you need to set a value for old data. - To remove a default value, use
ALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT; - Test on a development or staging environment before applying changes in production.
Update Existing Rows (Optional)
If you want to update old rows to match the new default:
UPDATE users SET status = 'active' WHERE status IS NULL;Conclusion
Adding default values in PostgreSQL is a simple but powerful tool for improving data quality and simplifying application code. Remember to combine it with updates for existing rows if necessary.
Automate PostgreSQL reporting with DataReportive
Turn your PostgreSQL queries into scheduled reports delivered to your team or customers.