recently I experience on postgresql and found that \dt are not show all tables if those tables with the same name but under different schema.
example
test2.my_table1
test1.my_table1
if setting search_path, \dt will only showing the tables which inside the 1st priority schema.
this will be an very pain full to multi-tenant database management.
simple use command
\dt *.<tale_prefix>*
example:
\dt *.my_*
Related
I know I can run this:
grant select on all tables in schema whatever to "my_dev_group";
but the way I've built our CI system - we use lots of different schemas, so the schema is only known at the time of running the tests - not when the script is written. Ie we run the same script against lots of different test schemas and we set the schema name at the start...
so I am trying to achieve the equivalent of the above grant statement - but to operate on the current schema - without any success.
ie all of these were spectacularly unsuccessful
grant select on all tables in schema current to "my_dev_group";
grant select on all tables in schema current_schema() to "my_dev_group";
grant select on all tables to "my_dev_group";
is it possible to do without going into the world of executing dynamic sql, which I'd prefer not to
SQL doesn't have a notion of a “current schema”. Consequently, there is no syntax to change the permissions of all tables in the current schema.
You could loop through all schemas in the search_path or all schemas in the database and run a GRANT statement for each one. Make sure to skip system schemas like information_schema and pg_catalog.
Alternatively, you could use the current_schema() function to retrieve the first existing schema on your search_path and change permissions for that schema.
I am trying to drop the schema masterdata from a postgres database, but it does not work. I am using PostgreSQL 9.5. I have 2 databses, one of them has the masterdata schema, which I want to drop. Here is the structure (in DBeaver):
My first attempt was just to execute the SQL statement DROP SCHEMA masterdata in DBeaver, but it tells me, that such a schema does not exist (but it does show it, as we can see in the picture!). Maybe, it does not know, in which of the 2 databses to look for this schema? If so, then how to specify it? I was looking for a way to specify the database, but did not find anything.
However, my second attempt was to use psql and to type the command
psql -U postgres -d bobd -h localhost
So here I concretely specify, which databse to use! psql does not answer anything, it just asks for the next command. And when I type \dn to view the current schemas, then the schema masterdata is still there! Also, the data in the tables is still there. I tried the same with other users instead of the user postgres (also with the owner of the schema to remove), but the result is the same.
Any ideas, what I am doing wrong?
Your DBeaver session was probably connected to the wrong database (postgres?).
Do it from the psql session, that is easiest.
Right after \dn showed you that there is indeed such a schema, enter
DROP SCHEMA masterdata;
It may complain that there are objects in that schema. Then you can use
DROP SCHEMA masterdata CASCADE;
Maybe your schema name contains characters that do not display or display differently (maybe capital letters) in DBeaver. I suggest you check names by running following query:
SELECT '~~' || nspname || '~~' FROM pg_catalog.pg_namespace;
Try adding quotes around schema name if you have added capital letters when creating schema.
In a given schema we can see the following tables:
The search_path is :
aact=# set search_path to public, ctgov ;
SET
The problem is that in the middle of a psql session a number of these tables just started showing zero rows. Then I exit'ed psql and restarted it: again those tables are still empty.
The interesting thing is this is not affecting all tables: see below:
I am not a regular user of postgres and have no clue what happened / is happening here. What should I look for?
From https://stackoverflow.com/a/17943883/156458
in both Postgres and the SQL Standard we have this containment
hierarchy:
A computer may have one cluster or multiple.
A database server is a cluster.
A cluster has [catalogs][8]. ( Catalog = Database )
Catalogs have [schemas][9]. (Schema = [namespace][10] of tables, and security boundary)
Schemas have [tables][11].
Tables have [rows][12].
Rows have values, defined by [columns][13].
In postgresql, I have a database called students and there is a table called student under it:
postgres=# \c students
You are now connected to database "students" as user "postgres".
students=# \dt;
public | student | table | postgres
I was wondering if database students is also a catalog?
Where is the schema between students and student?
In general, how can I list
all the catalogs and
all the schemas under a catalog, and
all the tables under a schema?
How can I show
the current catalog, and
the current schema?
Thanks.
In shortly, I usually explain to my junior teammate when they starting to research about PostgreSQL.
In PostgreSQL:
A Database Server is like an Industrial Zone, there will have many Databases-Cluster (Building)
Each Database-Cluster (Building) have many Databases. Those are like the floors in Building. The Databases is here as you call the Catalogs. Those Databases-Catalogs (floors) are quite independent with each other, you can not using others materials directly, you must use somethings just like stair, electric wire ... (in database is DBLINK).
Okay, next, each Database-Catalogs have many Schemas, which are like many Rooms on your floors. Those Schemas can use the material from each others.
Then, each Schemas have many cell elements such as Table, View, Function, Sequence .... All schemas have the same structure.
Now, back to you example:
students: is Database (which you call Catalogs)
public: is schema.
student: is table.
public | student | table | postgres is corresponding with schema | table | kind of table | owner of table
You can list:
catalogs (Database) by the command \l in psql or query select * from pg_database;
Schemas under a catalog by the command \dn in psql or query select * from information_schema.schemata;
Tables under a schemas by the query select * from pg_tables WHERE schemaname = 'Your schema';
You can show:
Current Database (Catalogs) by the query select current_database();
Current Schema by the query select current_schema;
Please pay attention that PostgreSQL have two system schema call information_schema and pg_catalog, this maybe make you confuse.
The pg_catalog is a system schema. More information.
The system catalogs are the place where a relational database management system stores schema metadata, such as information about tables and columns, and internal bookkeeping information. PostgreSQL's system catalogs are regular tables. You can drop and recreate the tables, add columns, insert and update values, and severely mess up your system that way. Normally, one should not change the system catalogs by hand, there are always SQL commands to do that. (For example, CREATE DATABASE inserts a row into the pg_database catalog — and actually creates the database on disk.) There are some exceptions for particularly esoteric operations, such as adding index access methods.
The information_schema is a system schema. More information.
The information schema consists of a set of views that contain information about the objects defined in the current database. The information schema is defined in the SQL standard and can therefore be expected to be portable and remain stable — unlike the system catalogs, which are specific to PostgreSQL and are modeled after implementation concerns. The information schema views do not, however, contain information about PostgreSQL-specific features; to inquire about those you need to query the system catalogs or other PostgreSQL-specific views.
I hope these information will help you clearly.
I'm using Esqueleto with Postgres and I don't see a way to specify the schema that a table resides in. Currently I'm issuing the following sql to set the schemas:
set search_path to foo,bar;
This allows me to use the tables I want as long as they are in schema foo or schema bar. Is there a way to just set the schema for each table?