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?
In Postgres a database is the highest level "container". Then schemas follow. There can be schemas with the same name in different databases (but no in the same database). The other databases won't "see" them. So there is no problem in your setup.
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:
Related
So basically I have 2 superusers, postgres and eric, each with their own databases. What I want to do is, while being connected to one of them, access the database (tables to be more precise) of the other. The tables of both databases are in public schema.
I am using this query, which I found on another question on the forum, but without any result:
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
I have changed the owners of the databases, offered all the privileges to both roles, but nothing. All I get are only the tables of the database under that user, not both.
Any idea what I could be missing? Thanks.
P.S: I am using PostgreSQL 9.3, and coding in Python 2.7
Superusers can always access everything in the whole cluster.
This sentence makes no sense:
Both databases are in public schema.
Cluster -> database -> schema -> table. Start with the manual here.
While connected to a particular database you can only access tables of this particular database. You have to connect to a different database to work with tables there.
Or you can use dblink or FDW.
PostgreSQL has a firewall between different databases. They might as well be on totally separate servers. You can use dblink or fdw or something like those to bridge between them.
I read a write up about database schema.
A SQL Server schema is a container of objects. For example you may have a large enterprise application and then is a good practice to use different schemas for different purposes (e.g. put HR related tables into HR schema, accounting related tables into Accounting schema and so on). A schema can be owned by any user, and the ownership is transferable.
They said: use different schemas for different purposes (e.g. put HR related tables into HR schema, accounting related tables into Accounting schema and so on)
Do they mean create new database for HR and again new database for accounting?
Because when we create a database then a single schema is created so we cannot create multiple schema in single SQL Server database as far I know.
So please tell me how is it possible to create different schemas for different purposes in a single database? Thanks
Purpose of Schema
Schemas in sql server were introduced in sql server 2005, The main purpose was to eliminate User's ownership of objects in sql server. or you can say to separate users from objects in sql server.
Prior to Sql server 2005 objects in sql server (Tables, views, Store proceders etc) were owned by users. Typically the user who created it.
And that user had to give permissions to other users to use that particular object.
Imagine a scenario where 12 developers are working in a company and all developers are creating sql objects left, right centre. Now all the developers had to give permissions to other 11 developers if they had to work objects created by that one developer. quite a bit of mess isnt it??
Since sql server 2005 came with Schema. All the objects were Owned by a Schema Not a User. if you havent created any custom schema it will be under default Schema dbo.
Now anyone who has permission to dbo schema has permission to any object under dbo schema.
Why it is a good idea to create different schemas for different departments in your case. It may be because HR people doesnt need to know anything about Finance stuff. so you can create a HR schema and give HR people permission only on HR schema. and vice versa with finance people. That will restrict their access to only objects related to their departments.
And we can create multiple Schemas in one database if you have ever worked with Adventureworks database, it has Schemas like 'Production', 'Sales' etc etc.
Read here to learn more about schemas in sql server.
No they mean create a schema. Create schema works within a database. There are all sorts of uses for it, I tend to think of it as either namespacing or a more natural way of partitioning a smallish database and keeping role based access, where you can think of schema as a user group.
Unfortunately, there are two meanings to the word "schema" in the database world.
One means the overall design of the database tables. "Show me your database schema", for example. This would be the collection of "create table" commands, or and ERD diagram.
The other is a synonym for "namespace", which the article in question is referring to. You can store tables, functions etc in different namespaces to ease cognitive load or use for security grouping.
is there a way to group tables into Postgres's schema like structure? We have a Postgresql server we want to move to Oracle DB. We use a lot of schemas to categorize tables. Anything Similar in oracle? Oracle has Schemas but there is a one to one relation between schema and a user. Schema == User.
Is there another stack-exchange that might be more appropriate for this question?
You will need to create the same amount of schemas in Oracle as you did in PostgreSQL. The fact that each schema is also associated with a user should not bother you.
You don't have to log-in with all those users if that's what you are wondering.
Just create one "application" user, and grant the necessary privileges on the tables you create in the various schemas to that application user.
If you used PostgreSQL's search path feature to avoid fully qualified table names, then you'll need to create synonyms (owned by the "applicatoin" user) that point to the tables in the various schemas.
Is it possible to have same table name on different schema with different data on the tables within the one database? I think rather than create multiple database (that hold same table name) i should create multiple schema instead.
Something like:
Schema 1:
table A, table B
Schema 2:
table B, table X
PS: table B hold different data for each schema
And most basic question did every schema (user) hold different table set? In SQL Server and MySQL every user on same database have same table set, the difference was only on roles, how about Oracle?
Thanks
Yes this is possible. A schema in Oracle is what a MySQL user would call a database.
However, you need to be careful when accessing either of the tables now if the user you connect with to the database instance has access to both. To unambiguously reference a table in a specific schema, use schema.table.
Here's the documentation on namespaces: https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/Database-Object-Names-and-Qualifiers.html#GUID-3C59E44A-5140-4BCA-B9E1-3039C8050C49
As jackrabbit says objects in different schemas have different namespaces.
Each schema in the database has its own namespaces for the objects it contains. This means, for example, that two tables in different schemas are in different namespaces and can have the same name.
Within a schema things are a little more complex.
Because tables and views are in the same namespace, a table and a view in the same schema cannot have the same name. However, tables and indexes are in different namespaces. Therefore, a table and an index in the same schema can have the same name.
I'm having trouble determining the difference between CREATE SCHEMA and CREATE DATABASE. I'm of the understanding that they are the same and that it's usual to use the latter.
Is that so?
Thanks!
They are definitely not that same! A database may consist of several schemas. Schemas are basically securable objects that can contain other securable objects such as tables, views, procedures etc. Securable in this context means something that is owned by someone and to which operations can be granted.
In MySQL, the two commands are synonymous from 5.0.2 upwards - this is perhaps why you had the understanding they were the same.
http://dev.mysql.com/doc/refman/5.0/en/create-database.html
However, as the others have mentioned, Schemas and Databases are different types of entity in other RDBMS.
CREATE schema is to create a schema above the database. So can a CREATE SCHEMA create tables, views, etc etc.
CREATE SCHEMA can't create a database.
Create schema creates new schema while create database creates database. See this link for more information about schemas: User-Schema Separation