AWS RDS Aurora PostgreSQL Table Import from S3 Stub

February 5, 2024

Here is the aws_s3.table_import_from_s3 stub that pairs nicely with the previously blogged aws_s3.query_export_to_s3 stub.

CREATE SCHEMA IF NOT EXISTS aws_s3;
CREATE OR REPLACE
  FUNCTION aws_s3.table_import_from_s3 
    (table_name TEXT, column_list TEXT, opt TEXT,
     bucket TEXT, path TEXT, region TEXT, OUT result TEXT)
AS
$$
DECLARE copy_stmt TEXT;
  rows BIGINT;
BEGIN
  copy_stmt := 'COPY ' || table_name;
  IF column_list IS NOT NULL AND length(column_list) > 0 THEN
    copy_stmt := copy_stmt || ' (' || column_list || ')';
  END IF;
  copy_stmt := copy_stmt || ' FROM ''' || path || 
    ''' WITH ' || opt || ';';
  EXECUTE copy_stmt;
  GET DIAGNOSTICS rows = ROW_COUNT;
  result :=  rows || ' rows imported into relation "' || 
    table_name || '" from file ' || path || 
    ' of ' || rows || ' bytes';
END;
$$
LANGUAGE plpgsql
;