Using postgres table description - postgresql

In postgres while listing the relation with \dt+ new columns 'size' and 'Description' column are added. As the name suggests Description can we use to store the description of the table, if yes how?
# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+-------
drs | Records | table | rho
drs | Reports | table | rho
(2 rows)
# \dt+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+---------+-------+-------+------------+-------------
drs | Records | table | rho | 8192 bytes |
drs | Reports | table | rho | 0 bytes |
(2 rows)

This can be defined using the comment statement:
comment on table drs."Reports" is 'This table stores reports';

Related

Apache AGE - How to delete a label table properly?

From AGE's source code, there is a function from the label_commands.c file which is called drop_label and I suppose that it deletes the table containing the label name. However, when I delete a vertex that is the only vertex of a label, it's table still appears at ag_catalog.ag_label (which stores all the available labels present in the graphs).
Here is the ag_catalog.ag_label table before deleting the only vertex from demo."Comic":
demo=# SELECT * FROM ag_catalog.ag_label;
name | graph | id | kind | relation | seq_name
------------------+-------+----+------+-----------------------+-------------------------
_ag_label_vertex | 16940 | 1 | v | demo._ag_label_vertex | _ag_label_vertex_id_seq
_ag_label_edge | 16940 | 2 | e | demo._ag_label_edge | _ag_label_edge_id_seq
Person | 16940 | 3 | v | demo."Person" | Person_id_seq
Book | 16940 | 4 | v | demo."Book" | Book_id_seq
Comic | 16940 | 5 | v | demo."Comic" | Comic_id_seq
Author | 16940 | 6 | v | demo."Author" | Author_id_seq
AUTHOR_OF | 16940 | 7 | e | demo."AUTHOR_OF" | AUTHOR_OF_id_seq
Store | 16940 | 8 | v | demo."Store" | Store_id_seq
(8 rows)
Deleting the vertex:
demo=# SELECT * FROM cypher('demo', $$
demo$# MATCH (v:Comic)
demo$# DETACH DELETE v
demo$# $$) as (v agtype);
Showing the contents of demo."Comic" afterwards:
demo=# SELECT * FROM demo."Comic";
id | properties
----+------------
(0 rows)
Since all the vertices of this label were removed, shouldn't this label also have been deleted and not even show this table? If not, how can I delete this label with cypher commands?
You can use the drop label function.
SELECT drop_label('graph_name','label_name');
Do note that this is not a cypher command, rather an AGE-specific function.

Bulk update datatype of a column in all relevant tables

An example of some tables with the column I want to change.
+--------------------------------------+------------------+------+
| ?column? | column_name | data_type |
|--------------------------------------+------------------+------|
| x.articles | article_id | bigint |
| x.supplier_articles | article_id | bigint |
| x.purchase_order_details | article_id | bigint |
| y.scheme_articles | article_id | integer |
....
There are some 50 tables that have the column.
I want to change the article_id column from a numeric data type to a textual data type. It is found across several tables. Is there anyway to update them all at once ? Information schema is readonly so I cannot do an update on it. Other than writing inidividual alter statements for all the tables, is there a better way to do it ?

How to list all indexes of a table with their corresponding size in PostgreSQL?

I can view the total size of all indexes in a table with
SELECT pg_size_pretty (pg_indexes_size('table_name'));
and the size of a specific index with:
select pg_size_pretty(pg_relation_size('index_name'));,
but I would like to retrieve a list with size information for each index of the table separately (a list of index sizes with the corresponding index name they belong to).
Use pg_indexes.
select indexname, pg_size_pretty(pg_relation_size(indexname::regclass)) as size
from pg_indexes
where tablename = 'my_table';
You can use \di+ psql command:
postgres=> \di+ schema.*
List of relations
Schema | Name | Type | Owner | Table | Persistence | Size | Description
--------+--------+-------+-------+----------------------+--------+-------------
schema | index1 | index | owner | table1 | permanent | 139 MB |
schema | index2 | index | owner | table1 | permanent | 77 MB |
schema | index3 | index | owner | table1 | permanent | 73 MB |
schema | index4 | index | owner | table1 | permanent | 38 MB |
(4 rows)

PostgreSQL: accessing relation with "public" schema name [duplicate]

This question already has answers here:
I keep getting the error "relation [TABLE] does not exist"
(1 answer)
Cannot simply use PostgreSQL table name ("relation does not exist")
(18 answers)
Setting schema in PostgreSQL JDBC doesn't seem to work
(1 answer)
Closed 4 years ago.
Using PostgreSQL 9.6, I created new table using pgAdmin under public schema.
But to access the table, I can't use just the table name. If I use public."Book" then only the table is accessible.
thrillio=# show search_path;
search_path
-------------
public
(1 row)
\d+ shows all the tables..
thrillio=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+-------------------+-------+----------+------------+-------------
public | Book | table | postgres | 8192 bytes |
public | KidFriendlyStatus | table | postgres | 0 bytes |
public | Movie | table | postgres | 8192 bytes |
public | User | table | postgres | 8192 bytes |
public | Weblink | table | postgres | 8192 bytes |
(5 rows)
thrillio=# select * from Book;
ERROR: relation "book" does not exist
LINE 1: select * from Book;
^
thrillio=# select * from public."Book";
id | title | profileUrl | publicationYear | publisher | authors | genre | amazonRating
----+-------+------------+-----------------+-----------+---------+-------+--------------
(0 rows)
is this expected? Anything I need to change the search_path to fix this?

How can I get all roads/ways from an osm2pgsql shema?

I'm trying to create a list of every road in Germany (With latitude,longitude and Streetname).
So far I have imported the german osm file with the help of osm2pgsql into a postgres Database.
So what I am looking for is a query that allows me to query every road.
First of all I would suggest using the psql tool. So assuming your database is called gis enter the following command at the terminal:
psql gis
from within the psql tool you can query the database but first it is a g0od idea to get an idea of which tables were created by osm2psql, do this be entering:
\d
this should give you output like this:
List of relations
Schema | Name | Type | Owner
--------+--------------------+-------+----------
public | geography_columns | view | postgres
public | geometry_columns | table | gis
public | planet_osm_line | table | user
public | planet_osm_nodes | table | user
public | planet_osm_point | table | user
public | planet_osm_polygon | table | user
public | planet_osm_rels | table | user
public | planet_osm_roads | table | user
public | planet_osm_ways | table | user
public | spatial_ref_sys | table | gis
(10 rows)
There we can see a likely looking candidate in planet_osm_roads, enter:
\d planet_osm_roads
this will show you the structure of the roads table, which will look something like this:
Table "public.planet_osm_roads"
Column | Type | Modifiers
--------------------+----------+-----------
osm_id | bigint |
access | text |
addr:housename | text |
addr:housenumber | text |
addr:interpolation | text |
admin_level | text |
aerialway | text |
aeroway | text |
amenity | text |
....
waterway | text |
wetland | text |
width | text |
wood | text |
z_order | integer |
way_area | real |
way | geometry |
From there we can formulate our query. Something like:
SELECT osm_id,name,way FROM planet_osm_roads LIMIT 1;
will get you the first road in the list. The documentation at http://www.postgresql.org/docs/ will help you with decoding the way geometry string.