Why \dt isn't showing the table? (PostgreSQL) - postgresql

tried a lot, but it seems impossible now to list down the table
seeking for help badly
below is cmnds followed by the issue
logicsoft-internship::DATABASE=> \dn
List of schemas
Name | Owner
------------+----------------
heroku_ext | u8q98eik4pd35o
public | grmgsmxgscmlmc
(2 rows)
logicsoft-internship::DATABASE=> \dt
Did not find any relations.
logicsoft-internship::DATABASE=> \dt dauokokqc98tl4
Did not find any relation named "dauokokqc98tl4".
logicsoft-internship::DATABASE=> \dt dauokokqc98tl4.public
Did not find any relation named "dauokokqc98tl4.public"

As documented in the manual \dn shows a list of schemas (=namespace).
To show the tables in a specific schema (namespace), you need to provide a wildcard with the schema name - you used the owner which is something completely different.
\dt heroku_ext.*
\dt public.*

Related

Postgresql schema "public" does not exist

When I was working on postgresql database, I delete some users using command
=>set role "test-admin" ;
SET
=> drop owned by "test-admin" cascade;
DROP OWNED.
=> drop user "test-temp";
DROP ROLE
but when I do
\dn
List of schemas
Name | Owner
------+-------
(0 rows)
The public schema does not exist anymore. I can still see a bunch of users when run \du, I can also see names of database when do \l.
The error I saw is: "Error: could not find schema 'public' while looking for owner".
Looks like public schema was either dropped or not accessible. I did not drop schema explicitly.
My questions are:
why does public schema disappear?
will drop owner of schema public also drop public schema?
how can I bring it back?
Should I manually recreate it?
Since I can still see databases, how can I associate the database with public schema again?
I tried all the approaches I can think of (such as login as users with different privileges), the public schema is still no available.
Thanks for the help in advance.

PostgreSQL display all tables in different schema without setting search_path

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_*

How can I show the catalogs and schemas in postgresql?

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.

Postgres query fails without schema

THis doesn't work, resulting in ERROR: relation "user" does not exist
select * from "user"
This one does
select * from "dbo"."user"
Tables that aren't schema-qualified are searched for on the search_path. This doesn't search every schema. By default it only searches:
pg_catalog (implicitly always first, not listed on search_path)
pg_temp tablespaces (implicit, not listed on search_path)
A schema named after the current user, listed as $user in search_path
The public schema
If you want PostgreSQL to look elsewhere you have to modify the search_path to tell it so, or fully schema-qualify the name.
See the PostgreSQL manual on search_path.
Also, note that unlike some databases the name dbo has no particular significance in PostgreSQL. It's just another name.

Postgres not following search_path to locate tables

I'm not sure the exact point at which this starting happening ( I believe it may have been after launching pgadmin3 for the first time ). Basically this seemed to happen all of a sudden and everything seems to be configured correctly..
PostgreSQL seems to no longer be following the search_path to locate my tables.
\d
No relations found.
My search path has been set (persistently at that):
SHOW search_path;
"public, myschema1, myschema2"
(1 row)
Not only am I the owner of the tables in question, I have run:
GRANT ALL ON SCHEMA public to myusername;
GRANT ALL ON SCHEMA myschema1 to myusername;
GRANT ALL ON SCHEMA myschema2 to myusername;
The data is all there. I can see the schemas when running a \dn. I can also run queries if I fully qualify the schema and table names. The biggest issue is that already created functions that reference an unqualified table name won't work.
Can anyone think of something I might not have tried? Any thoughts as to what caused this issue to start happening?
Thanks for any help!
-hightech
Probably you entered search_path as one string
postgres=# set search_path to public, s1;
SET
postgres=# show search_path ;
search_path
-------------
public, s1
(1 row)
postgres=# set search_path to 'public, s1';
SET
postgres=# show search_path ;
search_path
--------------
"public, s1"
(1 row)
second setting is wrong and doesn't work.
I fixed a similar issue by GRANTing that user USAGE ON SCHEMA, logged on as the OWNER on that schema. Let me develop:
User story: I could reproduce this strange behaviour with Postgres 9.6, see console output below. How bizarre was that? I could \dt preprod.users, but \dt only will not issue anything! I could also SELECT on these tables, which was really confusing me.
(In search_path, note the space after the comma, automatically adjusted by PostgreSQL)
do_preprod=> -- I'm logged on as preprod_www_ro, a read-only user who belongs to readonly ROLE.
do_preprod=> SET search_path TO preprod,public;
SET
do_preprod=> SHOW search_path;
search_path
-----------------
preprod, public
(1 row)
do_preprod=> \dt
Did not find any relations.
do_preprod=> \dt users;
Did not find any relation named "users".
do_preprod=> \dt preprod.users;
List of relations
Schema | Name | Type | Owner
---------+-------+-------+-------------
preprod | users | table | preprod_www
(1 row)
Then, logged on as preprod_www (= schema owner), I GRANTed the readonly ROLE (preprod_www_ro's) the USAGE privilege.
do_preprod=> GRANT USAGE ON SCHEMA preprod TO readonly;
Then, back as preprod_www_ro, I could immediately see the relations with \dt, enjoying fully my search_path, without having to log out log back in.
I would have liked a clearer message from PostgreSQL in the first place, when issuing \dt, or \dt preprod.users: "No USAGE ON SCHEMA granted..." or something like that, as in fact that user COULD use it...but not entirely smoothly.