mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add List All Columns Of A Specific Type as a postgres til.
This commit is contained in:
@@ -60,6 +60,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
||||||
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
|
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
|
||||||
- [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md)
|
- [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md)
|
||||||
|
- [List All Columns Of A Specific Type](postgres/list-all-columns-of-a-specific-type.md)
|
||||||
- [String Contains Another String](postgres/string-contains-another-string.md)
|
- [String Contains Another String](postgres/string-contains-another-string.md)
|
||||||
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
||||||
- [Timestamp Functions](postgres/timestamp-functions.md)
|
- [Timestamp Functions](postgres/timestamp-functions.md)
|
||||||
|
|||||||
28
postgres/list-all-columns-of-a-specific-type.md
Normal file
28
postgres/list-all-columns-of-a-specific-type.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# List All Columns Of A Specific Type
|
||||||
|
|
||||||
|
We can access information about all the columns in our database through the
|
||||||
|
`information_schema` tables; in particular, the `columns` table. After
|
||||||
|
connecting to a particular database, we can list all columns (across all our
|
||||||
|
tables) of a specific type. We just need to know the schema of the tables we
|
||||||
|
are interested in and the type that we want to track down.
|
||||||
|
|
||||||
|
My application's tables are under the `public` schema and I want to track
|
||||||
|
down all `timestamp` columns. My query can look something like this
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select table_name, column_name, data_type from information_schema.columns where table_schema = 'public' and data_type = 'timestamp without time zone';
|
||||||
|
table_name | column_name | data_type
|
||||||
|
-----------------+-------------+-----------------------------
|
||||||
|
articles | created_at | timestamp without time zone
|
||||||
|
articles | updated_at | timestamp without time zone
|
||||||
|
users | created_at | timestamp without time zone
|
||||||
|
users | updated_at | timestamp without time zone
|
||||||
|
(4 rows)
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, I could look for both `timestamp` and `timestamptz` with a
|
||||||
|
query like this
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select table_name, column_name, data_type from information_schema.columns where table_schema = 'public' and data_type like '%timestamp%';
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user