1

I have multiple tables that are created in the same way (same columns, indexes, etc.) I would like to have one sql file for creating them all without duplicating the create statements.

Is there a way to use some kind of placeholder in sql file which would be substituted when executing the sql file with a parameter?

For example I would like to have below sql statement:

drop table if exists schema.%PLACEHOLDER%;
create table schema.%PLACEHOLDER%(id text, data text);

And execute such script with:

psql -f mysqlfile.sql -magic_parameter my_desired_table_name

Is this possible when executing PostgreSQL sql files, or maybe other way to achieve the same (except using sed)?

MJavaDev
  • 261
  • 3
  • 10

1 Answers1

4

Sincr you are using psql, you can use variables as follows:

drop table if exists schema.:placeholder;

The invocation is:

psql -f mysqlfile.sql -v placeholder=table_name
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263