
- PostgreSQL - Home
- PostgreSQL - Overview
- PostgreSQL - Environment Setup
- PostgreSQL - Syntax
- PostgreSQL - Data Types
- PostgreSQL - Operators
- PostgreSQL - Expressions
- PostgreSQL Database
- PostgreSQL - Create Database
- PostgreSQL - ALTER DATABASE
- PostgreSQL - Drop Database
- PostgreSQL - Loading Database
- PostgreSQL - Rename Database
- PostgreSQL - Select Database
- PostgreSQL - Show Database
- PostgreSQL Query Operations
- PostgreSQL - SELECT
- PostgreSQL - CREATE
- PostgreSQL - INSERT
- PostgreSQL - UPDATE
- PostgreSQL - DELETE
- PostgreSQL - ALTER TABLE Command
- PostgreSQL - WHERE Clause
- PostgreSQL - ORDER BY Clause
- PostgreSQL - GROUP BY
- PostgreSQL - HAVING Clause
- PostgreSQL - DISTINCT Keyword
- PostgreSQL - LIMIT Clause
- PostgreSQL - LIKE Clause
- PostgreSQL - WITH Clause
- PostgreSQL - AND & OR Clauses
- PostgreSQL - DROP TABLE
- PostgreSQL - Upsert
- TRUNCATE TABLE Command
- PostgreSQL JOINS & Schemas
- PostgreSQL Schemas
- PostgreSQL Joins
- PostgreSQL Data Integrity & Transaction
- PostgreSQL - Constraints
- PostgreSQL - Transactions
- PostgreSQL - Commit
- PostgreSQL - Rollback
- PostgreSQL - Views
- PostgreSQL Functions
- PostgreSQL - ALIAS Syntax
- PostgreSQL - Functions
- PostgreSQL - Useful Function
- PostgreSQL - MAX() Function
- PostgreSQL - MIN() Function
- PostgreSQL - SUM() Function
- PostgreSQL - COUNT() Function
- PostgreSQL - Array Function
- PostgreSQL - String Function
- PostgreSQL - Numeric Function
- PostgreSQL Operators
- PostgreSQL - UNION Operator
- PostgreSQL - INTERSECT Operator
- PostgreSQL - EXCEPT Operator
- PostgreSQL - ANY Operator
- PostgreSQL - ALL Operator
- PostgreSQL - EXISTS Operator
- PostgreSQL Interface
- PostgreSQL - C / C++
- PostgreSQL - Java
- PostgreSQL - PHP
- PostgreSQL - Perl
- PostgreSQL - Python
- Advanced PostgreSQL
- PostgreSQL - NULL Values
- PostgreSQL - Triggers
- PostgreSQL - Indexes
- PostgreSQL - Locks
- PostgreSQL - Sub Queries
- PostgreSQL - Auto Increment
- PostgreSQL - Privileges
- PostgreSQL - Date/Time Functions & Operators
- PostgreSQL - Errors & Messages
- PostgreSQL - Assert
PostgreSQL - Errors and Messages
In PostgreSQL, an error occurs when command fails due to missing data, incorrect syntax or database constraints. While message gives useful information, warning or notices about database constraints.
Syntax
Following is the basic syntax of RAISE statement in PostgreSQL −
RAISE level 'message';
Here,
- The level can be NOTICE, WARNING, or EXCEPTION.
- When EXCEPTION stops the execution, it raises an error.
How to Handles Errors?
To handle the error in PostgreSQL, use the EXCEPTION inside a BEGIN and END block.
Example 1
Here, we provide the simple query to understand the RAISE statement.
postgres=# DO $$ postgres$# BEGIN postgres$# RAISE NOTICE 'This is a test message!'; postgres$# END $$; NOTICE: This is a test message! DO
The above query obtained the following result −

Example 2
This example provides a relation error when the table name does not exist in the database, and the implementation of the query is given below, which shows some error statements as an output.
DO $$ BEGIN BEGIN INSERT INTO users (id, email) VALUES (1, 'user@example.com'); EXCEPTION WHEN unique_violation THEN RAISE NOTICE 'Email already exists!'; END; END $$;
Following is the output −

Common errors in PostgreSQL
In PostgreSQL, common errors include incorrect syntax, connection failures, missing columns or tables, and authentication issues. To understand these errors in detail, we provide three examples to understand the errors in detail.
Division by Zero
The error occurs when we try to divide a number by zero.
SELECT 10 / 0;
The above query produces the following result −

Table Does Not Exist
This type of error occurs when we try to access the table and it doesn't exist in the database.
SELECT * FROM Products;
The above query produces the following result −

Duplicate Key Value
When we try to insert the duplicate value into a column with UNIQUE constraints, it gives error.
-- Create the table CREATE TABLE ABC ( id SERIAL PRIMARY KEY, email TEXT UNIQUE ); -- Insert values into table INSERT INTO ABC (email) VALUES ('abc@example.com'); INSERT INTO ABC (email) VALUES ('abc@example.com');
The above query obtained the following output −

Key role of Debugging
Following are given some points to understand the key role of debugging −
- It improves the software quality.
- It saves time and resources.
- Debugging is used to identify errors such as syntax errors, logical errors, runtime errors, and semantic errors./li>
- The debugging process allows the developer to fix the error.
- It enhances the software's performance.
Difference between Errors and Messages
Here, you can learn the differences between errors and messages in PostgreSQL −
Feature | Error | Message |
---|---|---|
Definition | This results a failure in execution due to incorrect syntax, missing data, or data constraint. | It provide information, warning, or notices without stops execution. |
Effect on Execution | Stops the execution immediately. | It does not stop the execution but shows the information. |
Levels (Example) | EXCEPTION | NOTICE, WARNING, DEBUG, LOG |
Usage | This is used to handle serious issues that require immediate attention. | This is used for debugging, logging, or informing users. |
Statement | RAISE EXCEPTION 'This is an error!'; | RAISE NOTICE 'Successful!'; |