user2711722
user2711722

Reputation: 31

Creating Multiple Tables in PostgreSQL

I want to create multiple tables (more than 100 tables) in one go. I am trying to write a script using loop such as for loop for generating as many tables as I want. I am new in PostgreSQL. I would be grateful if someone could give me some tips.

Upvotes: 1

Views: 7789

Answers (2)

Twelfth
Twelfth

Reputation: 7180

My answer here has to be 'dont do it!' and change your architechture. Create a table called customer and have a customer_id along with some other info. Create a second table with the columns you want here and a 'customer_id' column to refer to the customer table. This format will allow you to store in two tables what you are trying to store in 100+ tables. You want to stardardize your data base design at this stage...otherwise you are setting yourself up for a nightmare in the not too distant future. Databases are not spreadsheets...

Upvotes: 1

wildplasser
wildplasser

Reputation: 44250

#!/bin/sh

(
for i in 0 1 2 3 4 5 6 7 8 9; do
for j in 0 1 2 3 4 5 6 7 8 9; do
        echo "SET search_path=tmp;"
        echo "CREATE TABLE barf${i}${j}"
        echo " ( id SERIAL NOT NULL PRIMARY KEY );"
done
done) | psql -U lutsername databasename

Upvotes: 3

Related Questions