Is it possible to define now()
as a start value for tztsrange
data type introduced in postgresql 9.2? I tried to use this
ALTER TABLE test_table ALTER COLUMN tstz_range SET DEFAULT '[now,infinity]'::tstzrange;
But in such expression now()
is evaluated into current time stamp by altering table and default value is despite always the same:
'["2012-09-18 15:00:47.334196+02",infinity]'::tstzrange;
Changing a Column's Default Value. To set a new default for a column, use a command like: ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77; Note that this doesn't affect any existing rows in the table, it just changes the default for future INSERT commands.
Set Column Default using pgAdminIn the popup, go to 'Columns' tab and click on the edit icon againt a column to change the default value, as shown below. Now, go to the 'Constraints' tab and set or change the default value. Click on the Save button to save the changes.
We can provide the default value of the current timestamp to a column in PostgreSQL by using the default keyword. We can provide default current timestamp value to the column at the time of table creation.
In a table definition, default values are listed after the column data type. For example: CREATE TABLE products ( product_no integer, name text, price numeric DEFAULT 9.99 ); The default value can be an expression, which will be evaluated whenever the default value is inserted (not when the table is created).
As stated in the documentation you can define a range with the according function:
SELECT tstzrange(now(), 'infinity', '[)');
┌────────────────────────────────────────────┐
│ tstzrange │
├────────────────────────────────────────────┤
│ ["2012-09-18 15:33:50.186351+00",infinity) │
└────────────────────────────────────────────┘
So you can easily create a default value using the function:
CREATE TABLE plop (
id serial PRIMARY KEY,
some_range tstzrange DEFAULT tstzrange(now(), 'infinity', '[)')
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With