PostgreSQL Creating an Insert Trigger which Remaps Columns

I'm wondering if I can use a trigger on a table to "ignore" columns that are in a COPY statement from STDIN but which are not in the target table. Sorry if the wording/syntax of the question is off, but here is and explanation of what I'm trying to say. I'm new to triggers so any advice is helpful.

I'm using the PostGIS Shapefile importer to copy shapefiles to the spatial tables in my PostgreSQL database.

This creates a COPY statement which contains all the fields in the shapefile something like:

COPY "public"."stations" ("column1","column2","column3","column4", geom) FROM stdin;

column1 and column2 are in the file but not in the target table, so the COPY fails.

Is there a way to create a trigger to create something that would have the same result as:

COPY "public"."stations" ("column3","column4", geom) FROM stdin;

Answers


No, you cannot skip columns that are present in the input file. This will error out, before triggers are even invoked. And you cannot use rules either. I quote the manual:

COPY FROM will invoke any triggers and check constraints on the destination table. However, it will not invoke rules.

You can either edit the file or use a temporary staging table:

  1. COPY to a temporary table with matching columns.

  2. Use INSERT to write the desired columns to the final target table(s) - or the whole range of SQL DDL commands for more sophisticated matters.


Need Your Help

Optimisation of error messages

c# database-design

I was wondering what is the best way to store error message than can have above 200 characters in a database and said error message can contain diverse parameters.

SGEN: An attempt was made to load an assembly with an incorrect format

c# .net tfs msbuild

I have a project that can build fine on my local machine, however, when I get TFS to build it, I receive the following error -