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 Remove a Default Value From a Column in PostgreSQL
PostgreSQL allows you to define default values for columns, which can help simplify data entry. But what if you need to remove an existing default? This tutorial walks you through the steps to safely remove a default value from a PostgreSQL column.
Why Remove a Default Value?
There are several reasons you might want to remove a default value:
- The default is no longer valid or relevant.
- Application logic has changed and defaults are now handled elsewhere.
- You want to enforce that values are always provided explicitly.
Step 1: Check the Current Default
Before making any changes, it’s good practice to check if the column has a default. You can do this by running the following query:
SELECT column_name, column_default
FROM information_schema.columns
WHERE table_name = 'your_table' AND column_name = 'your_column';
Step 2: Remove the Default Value
Use the ALTER TABLE
command with ALTER COLUMN ... DROP DEFAULT
to remove the default value:
ALTER TABLE your_table
ALTER COLUMN your_column DROP DEFAULT;
Example:
ALTER TABLE users
ALTER COLUMN status DROP DEFAULT;
Step 3: Verify the Change
After running the command, re-check the column definition:
SELECT column_name, column_default
FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'status';
You should see that column_default
is now NULL
.
Additional Tips
- Make sure no application logic depends on the default before removing it.
- Test the change in a development or staging environment before applying it in production.
- Consider adding
NOT NULL
constraints if you want to enforce that a value is always provided.
Summary
Removing a default value in PostgreSQL is straightforward using the ALTER TABLE ... ALTER COLUMN ... DROP DEFAULT
command. Always remember to check the current schema, communicate with your team, and test thoroughly to avoid unexpected issues.
If you found this tutorial helpful, check out our other PostgreSQL tutorials for more tips and best practices!