Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using now() as default value in tztsrange in postgresql 9.2

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;
like image 824
Peter Krejci Avatar asked Sep 18 '12 13:09

Peter Krejci


People also ask

How do I give a field a default value in PostgreSQL?

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.

How do I change the default value in pgAdmin?

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.

How do I set the default time in PostgreSQL?

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.

How does PostgreSQL default value work?

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).


1 Answers

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', '[)')
);
like image 197
greg Avatar answered Nov 15 '22 04:11

greg