0

I am trying to create tables from all the tables that have been imported into my foreign tables via import foreign schema. The reason I want to do this is simply to copy data from one PostgreSQL server database to another. I know that I won't get sequences and other table-related indexes, but that's alright. I don't wanna do a pg_dump, so I've been trying to do it this way, but so far I haven't had any luck with the below code.

When I run the function, the query is running, but the tables don't seem to start being created in my database and I am having a hard time figuring out if my function is actually working or not.

DROP FUNCTION gv.create_tables_from_foreign_schema(character varying,character varying);

CREATE OR REPLACE FUNCTION gv.create_tables_from_foreign_schema(_foreign_schema character varying, _local_schema character varying)
  RETURNS void AS
$BODY$
declare
    selectrow record;
begin
for selectrow in
select 'CREATE TABLE ' || quote_ident(_local_schema) || '.' ||quote_ident(t.table_name) || ' AS SELECT * FROM ' || quote_ident(_foreign_schema) || '.' ||quote_ident(t.table_name) || '' AS qry 
from (
     SELECT table_name 
     FROM information_schema.tables
     WHERE table_schema = _foreign_schema
     )t
loop
execute selectrow.qry;
end loop;
end;
$BODY$
  LANGUAGE plpgsql

SELECT gv.create_tables_from_foreign_schema('gv_import_fra_pgv_2018a', 'gv')

Does the function make sense to you?

aneh
  • 3
  • 1

1 Answers1

0

The function looks ok.

I suspect that everything is fine; you don't see any new tables because the transaction isn't finished yet. If you look into the data directory, you should see new files being created and written.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263