3

I want to create a new table using postgREST, but I was not able to find anything about this in the documentation. Is it possible to create tables? If yes, how?

Sorry if this questions was already asked, but unfortunately I always found solutions for postgres (without t :D)

Thx :)

Daniel P.
  • 43
  • 4
  • PostgREST is just a REST API for a PostgreSQL (or just Postgres) database and does not contain any tables. Any table you want to use, needs to be created in the database your postgREST installation connects to using a [create table](https://www.postgresql.org/docs/current/sql-createtable.html) statement –  Oct 14 '20 at 06:07
  • Thx for the answer. My question is: is there any REST call possible to create a table in my postgresql? I want to write a little controller which will create a table for me. – Daniel P. Oct 14 '20 at 06:27

1 Answers1

3

you can create stored function that creates the table for you, then call that function via postgrest

something like:

create or replace function create_foo_table() returns void as 
$$
begin

  execute format('create table foo (id int primary key)')

end 
$$ language plpgsql;

then call /rpc/create_foo_table in Postgrest

you'd need to reload Postgrest's schema cache after this, in order to read the table: https://postgrest.org/en/v7.0.0/admin.html?highlight=reload#schema-reloading

This likely has security implications, so be careful, especially if using dynamic SQL to create the table

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152