简体   繁体   中英

Create a function to insert columns from one table to another in PostgreSQL

I have GPS Data that gets stored in the default storage of GPS PLUS X from Vectronic. In GPS PLUS XI can copy that my PostgreSQL Database (stored in the table public.gps_plus_positions). From this table I would like to extract certain columns with a function and automatically insert into another table, every time the public.gps_plus_positions gets updated.

Therefore I run the following code (for the simple case of just one column to extract):

CREATE TABLE main.gps_test(
utc_date date)

CREATE OR REPLACE FUNCTION tools.gps_plus_positions2gps_test()
RETURNS trigger AS
$BODY$ begin
INSERT INTO main.gps_test(utc_date)
 SELECT 
    NEW.utc_date
;
RETURN NULL;
END
$BODY$
LANGUAGE plpgsql VOLATILE;
COMMENT ON FUNCTION tools.gps_plus_positions2gps_test()
IS 'Automatic upload data from gps_data to gps_test.';

CREATE TRIGGER trigger_gps_data_upload2test
  AFTER INSERT
  ON public.gps_plus_positions
  FOR EACH ROW
  EXECUTE PROCEDURE tools.gps_plus_positions2gps_test();
COMMENT ON TRIGGER trigger_gps_data_upload2test ON public.gps_plus_positions
IS 'Automatic upload data from gps_plus positions to gps_test.';

But as soon as I run that code, copying the data from the default storage in GPS PLUS X to the PostgreSQL module is not executed anymore. Hence I suppose there is an error in my code.

I'd be very grateful about any suggestions to fix this problem.

Cheers!

Solved. Looking in the log_files explained what was wrong. No problem with the connection to Vectronic GPS PLUS X software.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM