Re: Conditional INSERT

From: Michel Pelletier <pelletier(dot)michel(at)gmail(dot)com>
To: basti <mailinglist(at)unix-solution(dot)de>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Conditional INSERT
Date: 2019-03-15 18:10:04
Message-ID: CACxu=vKcVZdjsmMQyGf2tAcVAt8vyz2X1b+ynPb=_==AATjbVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Well, the obvious question is, why are you inserting data into your
database you don't want? It makes more sense to just not do the insert.

But, assuming perhaps you have no control over the client, you can create a
BEFORE INSERT trigger that rejects the inserts that don't match your
condition:

https://www.postgresql.org/docs/current/plpgsql-trigger.html#PLPGSQL-DML-TRIGGER

On Fri, Mar 15, 2019 at 10:55 AM basti <mailinglist(at)unix-solution(dot)de> wrote:

> Hello,
>
> I want to insert data into table only if condition is true.
> For example:
>
> INSERT into mytable (domainid, hostname, txtdata)
> VALUES (100,'_acme.challenge.example', 'somedata');
>
> The insert should only be done if Hostname like %_acme.challenge%.
>
> How can it be done? I dont want that the user/script can insert any value.
>
> Best regards.
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Lewis 2019-03-15 18:17:17 Re: Conditional INSERT
Previous Message basti 2019-03-15 17:55:34 Conditional INSERT