0

I hope someone can help me to learn about Postgres schema and databases.

I have Postgres database called db1 and it has table calls table1. all these objects are in default schema called public.

Then I created another database called db2 and created another table with same name table1.

I didn't got any error but both tables are in same schema called public.

I created indexes for both of these tables and they both refer the table as public.table1.

I wonder a setup like this will give issue or not? If yes, then should I create different schemas for each database if I'm going to use same table names in all databases?

2 Answers 2

3

In Postgres a database is the highest level "container". Then schemas follow. There can be schemas with the same name in different databases (but not in the same database). The other databases won't "see" them. So there is no problem in your setup.

Sign up to request clarification or add additional context in comments.

Comments

3

Can we have two Postgres databases in same schema?

The answer is no. The relevant PostgreSQL documentation shows the hierarchy:

database.schema.table

This diagram (taken from this answer) is useful to visualise the relationship:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.