In PostgreSQL, constants are similar to variables but with a crucial difference: once their value is initialized, it cannot be altered. This immutability is beneficial for several reasons, enhancing both, the readability of SQL queries and reducing maintenance efforts.
Here we will look into the syntax, usage, and practical examples of constants in PostgreSQL to provide a clear understanding of their importance and operation.
Syntax
constant_name CONSTANT data_type := expression;
Let’s analyze the above syntax:
- Constant Name: Typically written in uppercase letters, this follows the naming conventions similar to variables but is used to signify unchangeable values.
- CONSTANT Keyword: This explicitly declares the identifier as a constant.
- Data Type: Specifies the data type the constant holds, such as ‘NUMERIC’, ‘VARCHAR’, etc.
- Initialization: A constant must be initialized with a value at the time of declaration.
PostgreSQL Constants Examples
Let us take a look at some of the examples of Constants in PostgreSQL to better understand the concept.
Example 1: Calculating with Constants
The following example declares a constant named VAT for valued added tax and calculates the selling price from the net price:
DO $$
DECLARE
VAT CONSTANT NUMERIC := 0.1;
net_price NUMERIC := 20.5;
BEGIN
RAISE NOTICE 'The selling price is %', net_price * ( 1 + VAT );
END $$;
Output:

Now let’s attempt to change the constant as below:
DO $$
DECLARE
VAT constant NUMERIC := 0.1;
net_price NUMERIC := 20.5;
BEGIN
RAISE NOTICE 'The selling price is %', net_price * ( 1 + VAT );
VAT := 0.05;
END $$;
As expected it raises an error as shown below:

Explanation: This example clearly demonstrates how constants can simplify calculations in financial operations by keeping the tax rate fixed and visible.
Example 2: Time-Dependent Constants
It is important to note that PostgreSQL evaluates the value for the constant when the block is entered at run-time, not compile-time as shown in the below example:
DO $$
DECLARE
start_at CONSTANT time := now();
BEGIN
RAISE NOTICE 'Start executing block at %', start_at;
END $$;
Output:

PostgreSQL evaluates the NOW() function every time we call the block. To prove it, we execute the block again:

Output: Each execution of this block will capture and display the time at which the block starts running, showing how constants can dynamically initialize based on the current state when the block is entered.
Important Points About PostgreSQL Constants
- Using constants can improve the performance of queries by allowing PostgreSQL’s query planner to make optimizations based on the immutability of values.
- In complex procedures, using constants can simplify debugging and future maintenance. Since the value is set in one place, any changes required later need only be made once, reducing the risk of errors or inconsistencies.
- Unlike some other programming environments where constants must be defined with static values, PostgreSQL allows constants to be initialized dynamically using expressions or functions that are evaluated at run-time.
- Any attempt to reassign a value to a constant after its initial definition will result in a compilation error.
Similar Reads
PostgreSQL - UNIQUE Constraint
In PostgreSQL, the UNIQUE constraint is a powerful tool used to ensure that values stored in a column or a group of columns are unique across rows in a table. This constraint is essential for maintaining data integrity, especially when certain data should not be duplicated. For instance, if you're s
3 min read
PostgreSQL - INSERT
PostgreSQL INSERT statement is one of the fundamental SQL commands used to add new rows to a specified table within a PostgreSQL database. This command allows users to insert data efficiently, whether for a single record or multiple records at once. With the PostgreSQL INSERT INTO clause, we can spe
5 min read
PostgreSQL - NOT NULL Constraint
In PostgreSQL, the NOT NULL constraint is a fundamental feature to ensure that a column cannot contain NULL values. NULL represents unknown or missing information in databases, distinct from an empty string or the number zero. For example, if you ask someone for their email address and they donât kn
3 min read
PostgreSQL- CONCAT Function
The PostgreSQL CONCAT function allows us to combine multiple strings or column values into a single output, making it a flexible tool for data manipulation. This function is essential for string concatenation tasks, whether weâre working with static text, columns from a database table, or dynamic SQ
4 min read
PostgreSQL - Psql commands
PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively. Here, we highlight some of t
2 min read
PostgreSQL - Generate Columns
When working with databases, there are scenarios where you need a columnâs value to be automatically computed based on other columns. In PostgreSQL, this can be achieved through generated columns. These special columns are calculated based on an expression using other columns in the table. The value
3 min read
PostgreSQL - Alias
PostgreSQL aliases are powerful tools that allow you to assign temporary names to tables or columns within your queries. These aliases only exist during the execution of the query, making your SQL code more readable and efficient. What is a PostgreSQL Alias?An alias in PostgreSQL is a temporary name
2 min read
PostgreSQL Exercises
PostgreSQL is a powerful, open-source relational database system that supports complex queries, data types, and performance optimization features. This article provides PostgreSQL practice exercises with solutions, allowing learners to explore how joins, aggregations, triggers, and foreign keys work
15+ min read
PostgreSQL - Identity Column
In PostgreSQL, an identity column is a specialized column type that automatically generates unique values for each row, making it ideal for primary keys and other unique identifiers. Introduced in PostgreSQL 10, the GENERATED AS IDENTITY clause offers a SQL-standard alternative to the widely-used SE
4 min read
PostgreSQL - CHR Function
The CHR() function in PostgreSQL is used to convert an integer ASCII code or a Unicode code point into its corresponding character. Let us better understand the concept of CHR Function in PostgreSQL from this article. SyntaxCHR(value);Parameter:value: The 'value' argument is generally an integer ASC
2 min read