What does the output of \dt mean? - postgresql

postgres=# \c students
You are now connected to database "students" as user "postgres".
students=# \dt;
public | student | table | postgres
students=# \dt+
public | student | table | postgres | 0 bytes |
students=#
What does the output of \dt mean? I only only know there is a table student under database students beforehand.
I am using psql (9.6.6).
Thanks.

\dt (no parameters) appears to list all the tables in the current schema, as the docs here explain: http://www.postgresonline.com/special_feature.php?sf_name=postgresql83_psql_cheatsheet
Your output is one row showing the (one) student table. If I create one table, similarly, I get this:
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+--------
public | student | table | postgres
(1 row)
Do \dt+ to see slightly expanded information.

Related

Why \dt isn't showing the table? (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.*

show postgresql database schema

I want to show something like this with PostgreSQL but I don't know what I must searching for
can you explain to me how to show something like this with PostgreSQL ?
The image you shared is a graphical layout of tables. PostgreSQL doesn't produce such output, but you can get the layout of individual tables by running \d <tablename> in psql. This will provide you with the list of columns, column types, constraints, primary key, foreign keys and comments.
For example:
postgres=# \d catalogue
Table "public.catalogue"
Column | Type | Collation | Nullable | Default
-------------+---------+-----------+----------+---------------------------------------
id | integer | | not null | nextval('catalogue_id_seq'::regclass)
item | name | | |
qty | integer | | |
description | text | | |
Indexes:
"catalogue_pkey" PRIMARY KEY, btree (id)
Alternatively, if you require a GUI-based solution, check out PgAdmin: https://www.pgadmin.org/
You can use dbeaver for this, using View Diagram option clicking in you schema

ERROR: must be owner of type character varying or type json

I need to cast varchar to json using:
CREATE CAST (character varying AS json) WITHOUT FUNCTION as ASSIGNMENT;
Connecting to Postgres with the master account of my RDS Postgres instance, I get the following error
ERROR: must be owner of type character varying or type json
The owner is rdsadmin:
\dT+ varchar
List of data types
Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
------------+-------------------+---------------+------+----------+----------+-------------------+-------------------------------------------------------------------
pg_catalog | character varying | varchar | var | | rdsadmin | | varchar(length), non-blank-padded string, variable storage length
Now I don't have rdsadmin password, is there any other way to run this query?
I don't think you can bootstrap your way to the full rdsadmin privileges.
However it seems that you can make yourself the owner of types:
ALTER TYPE json OWNER TO myowner;
And then you can create casts for that type to your heart's content.

How to find all object in a database using a script?

Can someone help me with this? I need to find out all the objects in a database using a script. The reason why I need this is, I am asked to change the owner of all database objects so for that I need to first list down all objects.
I think you probably want REASSIGN OWNED instead. No need to identify all the objects, just the users.
If you're changing every object from role A to role B, you might like REASSIGN OWNED:
REASSIGN OWNED BY A TO B
In PostgreSQL (or almost any other RDBMS for that matter), I would recommend to take a look at metadata tables (system catalog).
Example. You want all tables:
db => \d pg_tables
View "pg_catalog.pg_tables"
Column | Type | Modifiers
-------------+---------+-----------
schemaname | name |
tablename | name |
tableowner | name |
tablespace | name |
hasindexes | boolean |
hasrules | boolean |
hastriggers | boolean |
db => select tablename from pg_tables;
Will get you a list of all tables. You can use a query to build a script to change ownership of the tables you want.
Similarly, you can query other views/tables in the catalog to get other object types (sequences, indexes, you name it).
If you can do a pg_dump and pg_restore to create a new database the adding the --no-owner flag on the pg_restore and running the pg_restore as the user you wish to set ownership to should work for this.

Why can't I see the column names of a table?

So I made some tables programatically and I want to verify it's structure is what I think it is.
>Rocko=# \c Rocko
Password for user Rocko:
psql (8.4.4, server 8.4.8)
You are now connected to database "Rocko".
Rocko=# \d
List of relations
Schema | Name | Type | Owner
--------+--------------+----------+-------
public | Email | table | Rocko
public | Email_id_seq | sequence | Rocko
public | Test | table | Rocko
public | Test_id_seq | sequence | Rocko
public | User | table | Rocko
public | User_id_seq | sequence | Rocko
(6 rows)
So, for example, I want to see the columns in Test.
Rocko=# \d Test
Did not find any relation named "Test".
Wait a minute?! Did not Test just show up under "List of Relations"? What's going on here?
PostgreSQL converts unquoted identifiers (such as table and column names) to lower case by default; the standard says that identifiers are supposed to be normalized to upper case but that's not important here. So, when you say this:
\d Test
PostgreSQL considers that the same as \d test. You probably have a table that was created with a quoted name:
create table "Test" ( ...
so that its name is case sensitive and must be quoted (with double quotes) every time it is referenced. So try quoting the name:
\d "Test"