Skip to content

Commit 8f64588

Browse files
committed
Add List All Columns Of A Specific Type as a postgres til.
1 parent 864def4 commit 8f64588

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
6060
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
6161
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
6262
- [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md)
63+
- [List All Columns Of A Specific Type](postgres/list-all-columns-of-a-specific-type.md)
6364
- [String Contains Another String](postgres/string-contains-another-string.md)
6465
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
6566
- [Timestamp Functions](postgres/timestamp-functions.md)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# List All Columns Of A Specific Type
2+
3+
We can access information about all the columns in our database through the
4+
`information_schema` tables; in particular, the `columns` table. After
5+
connecting to a particular database, we can list all columns (across all our
6+
tables) of a specific type. We just need to know the schema of the tables we
7+
are interested in and the type that we want to track down.
8+
9+
My application's tables are under the `public` schema and I want to track
10+
down all `timestamp` columns. My query can look something like this
11+
12+
```sql
13+
> select table_name, column_name, data_type from information_schema.columns where table_schema = 'public' and data_type = 'timestamp without time zone';
14+
table_name | column_name | data_type
15+
-----------------+-------------+-----------------------------
16+
articles | created_at | timestamp without time zone
17+
articles | updated_at | timestamp without time zone
18+
users | created_at | timestamp without time zone
19+
users | updated_at | timestamp without time zone
20+
(4 rows)
21+
```
22+
23+
Alternatively, I could look for both `timestamp` and `timestamptz` with a
24+
query like this
25+
26+
```sql
27+
> select table_name, column_name, data_type from information_schema.columns where table_schema = 'public' and data_type like '%timestamp%';
28+
```

0 commit comments

Comments
 (0)