smk
smk

Reputation: 361

Create 1000 tables using script in Postgresql

I need to create 1001 tables, table names should be 0-1000 and each table has two double value columns called A and B. How do I create this many tables without executing 1001 CREATE TABLE queries manually? I am using Postgresql on linux.

Upvotes: 1

Views: 599

Answers (1)

SantiM
SantiM

Reputation: 312

I am sure there is a best way to do this, but you could generate the statements in shell script and then just load the sql.

Something like

#!/bin/sh
NAME="name"
COLUMNA="ca"
COLUMNB="cb"

for i in `seq 0 1000`;
  do
    echo "CREATE TABLE $NAME$i ($COLUMNA varchar(200), $COLUMNB varchar(200));"
  done

Then you just execute sh script.sh > creation.sql and load it with Postgres.

Upvotes: 3

Related Questions