Get PostgreSQL Database Structure as a Detailed JavaScript Object
pg-structure.com
pg-structure.com
information_schema doesn't cover anything the DBMS is doing that's not standardized by SQL, though. This would mean that, for PG in particular, things like partitioning/inheritance, tablespaces, and the distinction between roles and schemas, aren't represented in the information_schema. (The objects do show up, but only as their SQL-standard "superclass"—e.g. partitioned tables just look like a set of regular tables + constraints + triggers + rewrite rules; table columns with special types look like their underlying storage type; etc.)
The information_schema also doesn't cover the pragmatics of the administration of the database instance itself, like, for PG, the type of data you'd access through any of the pg_stat_ tables.
But neither of these concerns are really relevant if you're building a generic tool that wants to prod at SQL-standard database objects, I would think. Just use the information_schema!
---
I would like to link here the relevant part of the SQL standards document, because it's actually a very helpful and thorough reference. (Even if you never program for more than one DBMS, just learning how it defines words like "catalog", "schema", and "object" will make reading any particular DBMS's docs 10x easier.)
But, sadly, the SQL standard itself is a proprietary document that you have to purchase! (See here: https://modern-sql.com/standard) This is a pretty odd thing, considering that unpaid developers of FOSS systems like PG need to reference the standard for compliance.
This is a massive problem with standards in general. ISO 8601, for example, the international standard for date and time representation that everyone should be using, similarly consists of multiple documents that you have to purchase at significant prices. The part covering "basic rules" costs 158 CHF (160 USD) and the part covering "extensions" costs 178 CHF (180 USD).
I think this is seriously hampering the adoption of standards outside of industries that explicitly require compliance, and particularly in open source. If you can't even know whether you're compliant without spending serious money, why care about compliance at all?
https://github.com/aquametalabs/meta http://blog.aquameta.com/intro-meta/
information_schema is pretty unruly, and pg_catalog is just crazy, in terms of readability, but if you just want to do some simple introspection, meta might be helpful.
That said, I see value in a nice Javascript object that is easy to traverse and can be retrieved all at once.
The nice aspect of pg-structure for me was getting all of that information_schema goodness already pulled out into hashes/DTOs from a 1-line "await pgStructure(...)" call.
I.e. w/o pg-structure, I'd probably end up writing a mini/hacky version of it that did the same thing, "do these ~3-4 SQL queries and mash them into some nice hashes for my code generator to consume".
Which is not terrible, but it's easier to just pull in another npm package. :-)
This will also likely reflect the current version of the database engine you are working with, there's no consistent approach to know everything for all databases for all versions.
https://pypi.org/project/sqlacodegen/
Note that SQLAlchemy itself offers this functionality in core, codegen just makes it more human readable static files.
I used it to document a 1000-table db, and then added autogenerated docstrings for each table with # of rows/columns, list of the column names, and the contents of its first and last 5 rows.
This was invaluable in understanding a decade of legacy system development...
I'm curious, does anyone here use it that would care to explain their use case?
It's a last minute check that prevented a lot of mistakes.
None of those things actually being a problem could give you false positives, so you might want some minor shuffling.
So, yeah, just custom/minor infrastructure/tooling stuff that is based on the db schema.
I then use this data in my database diffing project `migra`, to autogenerate database migrations. You can also use schema diffing to test that your production and development databases match explicitly.
Overall it's a much more flexible but rigorous approach than the old-fashioned rails/django migrations.
If you _do_ really mean "pgsql > json` and not `pgsql -to-> json` then maybe I'm just confused.
I still think it's a little confusing but I understand why it is that way at least.
https://dev.mysql.com/doc/refman/8.0/en/information-schema.h... https://www.postgresql.org/docs/12/information-schema.html
As it happens, I did a talk about this at re:Clojure this week. Videos aren't up yet but they will appear here when they are available
https://www.youtube.com/channel/UCbZW8yCqEncYciie8_1yy7w/fea...
Also why Javascript?