Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want define my own message for each constraint. Is it possible to customize constraint violation messages in PostgreSQL?

share|improve this question
up vote 10 down vote accepted

This has been asked in Postgres Forum: Custom Constraint Violation Errors

and after some discussion:

So am I got that right, that If I want to have eg custom error messages for a foreign key violation, I need to replace all fkeys by trigger functions?!

Michael Musenbrock

answered there:

The short answer is, while it is possible to custom make your own constraint triggers that emulate foreign keys, don't do it. This would be a maintenance nightmare.

I'd be more maintainable to catch these errors in your client application. Here you would reword these error messages according the business rules of your client application.

Richard Broersma Jr.

with what I think would work best:

It's not hard to create:

CONSTRAINT some_constraint_name FOREIGN KEY col REFERENCES blah(id)

... then in the app, match "some_constraint_name" and map it to a suitable error. That's what I do and it works very well for all constraint types, not just foreign key constraints.

Craig Ringer

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.