extracting meta info from a table psql using information_schema - postgresql

How do I extract following meta information from a psql table. column_name, data_type, char_max_length,numeric_precision,constraint_type, constraint_reference, check_clause .In short the information that we have got by this query "\d table_name".

Start psql with the -E flag, and take note of the queries it generates when you issue \d table_name -- you'll need to reproduce many or most of those queries in order to get all the metadata you're asking for.

Related

How to add ONE column to ALL tables in postgresql schema

question is pretty simple, but can't seem to find a concrete answer anywhere.
I need to update all tables inside my postgresql schema to include a timestamp column with default NOW(). I'm wondering how I can do this via a query instead of having to go to each individual table. There are several hundred tables in the schema and they all just need to have the one column added with the default value.
Any help would be greatly appreciated!
The easy way with psql, run a query to generate the commands, save and run the results
-- Turn off headers:
\t
-- Use SQL to build SQL:
SELECT 'ALTER TABLE public.' || table_name || ' add fecha timestamp not null default now();'
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema='public';
-- If the output looks good, write it to a file and run it:
\g out.tmp
\i out.tmp
-- or if you don't want the temporal file, use gexec to run it:
\gexec

How to exec against a loaded splayed table in KDB?

I have a splayed table written to disk via .Q.dpft[]. After load this table using the \l system command, I can select from this table, e.g., select column_name from splayed_table where xyz
However, when I convert select to exec, I always get the 'nyi error: Not yet implemented. As a workaround, I use: first value flip instead of exec
I am doing something wrong?
Is there a better way?
Note: My KDB+ is KDB+ 3.3 2016.03.14 (Linux 64-bit).
exec cannot be used against a splayed table. A more efficient workaround may be to do exec column_name from select column_name from splayed_table

Select all tables in psql database EXCEPT those matching [pattern]

I have a database with many tables that are used as a reference for valid values in the other tables. These reference tables are all named valid[table], so I can display them in psql with \dt valid*.
I'd like to be able to select all the non-reference tables (which are more variably named, but none of them start with valid), but can't figure out how. I've tried various things like \dt !valid* , \dt !~valid* , \dt NOT LIKE 'valid%', but these either error or don't find a match.
Any suggestions would be appreciated!
According to the manual, psql supports these patterns. Normally you could use regular expression negative lookahead (?!valid)* but ? cannot be used in regular expression with psql because it is translated to .. It is probably easier to do a query from INFROMATION SCHEMA:
SELECT table_name FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema') --exclude system tables
AND table_type = 'BASE TABLE' -- only tables
AND table_name NOT LIKE 'valid%';

How can a list of table's field names be queried from PostgreSQL?

How can a plain text list of the field names of table be retrieved from PostgreSQL database?
Just query INFORMATION_SCHEMA.COLUMNS, like this:
SELECT
column_name,
data_type,
character_maximum_length,
ordinal_position
FROM information_schema.columns
WHERE table_name = 'mytable'
Better still, INFORMATION_SCHEMA is almost universally supported by all popular SQL databases, so this should work anywhere.
If you really want just dump plain text file recipe, you can execute this query using command line psql and save it as CSV or something like that.

How to find out the availability of datatype in Postgres

Is there any way to find out particular datatype [say for example chkpass] is available or not through a query ?
PostgreSQL provides a huge amount of meta-data which is easily accessed via SQL. To obtain information about the presence of a (scalar) data type, the information provided by pg_type catalog might be of interest here. Try, for example:
SELECT COUNT(*)
FROM pg_type
WHERE typname = 'chkpass'
run: psql -E
and then, in psql session \dT or \dTS