mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Table Names Are Treated As Lower-Case By Default as a Postgres TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1494 TILs and counting..._
|
_1495 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -839,6 +839,7 @@ _1494 TILs and counting..._
|
|||||||
- [Survey Of User-Defined Ordering Of Records](postgres/survey-of-user-defined-ordering-of-records.md)
|
- [Survey Of User-Defined Ordering Of Records](postgres/survey-of-user-defined-ordering-of-records.md)
|
||||||
- [Switch Non-Castable Column Type With Using Clause](postgres/switch-non-castable-column-type-with-using-clause.md)
|
- [Switch Non-Castable Column Type With Using Clause](postgres/switch-non-castable-column-type-with-using-clause.md)
|
||||||
- [Switch The Running Postgres Server Version](postgres/switch-the-running-postgres-server-version.md)
|
- [Switch The Running Postgres Server Version](postgres/switch-the-running-postgres-server-version.md)
|
||||||
|
- [Table Names Are Treated As Lower-Case By Default](postgres/table-names-are-treated-as-lower-case-by-default.md)
|
||||||
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
||||||
- [Temporary Tables](postgres/temporary-tables.md)
|
- [Temporary Tables](postgres/temporary-tables.md)
|
||||||
- [Terminating A Connection](postgres/terminating-a-connection.md)
|
- [Terminating A Connection](postgres/terminating-a-connection.md)
|
||||||
|
|||||||
80
postgres/table-names-are-treated-as-lower-case-by-default.md
Normal file
80
postgres/table-names-are-treated-as-lower-case-by-default.md
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
# Table Names Are Treated As Lower-Case By Default
|
||||||
|
|
||||||
|
This one is a bit unintuitive and can cause some real confusion -- when you
|
||||||
|
create a table in PostgreSQL, any casing is ignored, it is treated as
|
||||||
|
lower-case. Let's see it to believe it:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> create table BookMarks (
|
||||||
|
id integer generated always as identity primary key,
|
||||||
|
location text not null
|
||||||
|
);
|
||||||
|
|
||||||
|
> \d
|
||||||
|
+--------+--------------------+----------+----------+
|
||||||
|
| Schema | Name | Type | Owner |
|
||||||
|
|--------+--------------------+----------+----------|
|
||||||
|
| public | bookmarks | table | postgres |
|
||||||
|
| public | bookmarks_id_seq | sequence | postgres |
|
||||||
|
+--------+--------------------+----------+----------+
|
||||||
|
```
|
||||||
|
|
||||||
|
Notice that when we list our tables, the uppercase `M` and `B` are gone. That's
|
||||||
|
because Postgres folds away the casing when processing the table name
|
||||||
|
identifier.
|
||||||
|
|
||||||
|
It doesn't matter how we refer to it for queries:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select * from BookMarks;
|
||||||
|
+----+----------+
|
||||||
|
| id | location |
|
||||||
|
|----+----------|
|
||||||
|
+----+----------+
|
||||||
|
|
||||||
|
> select * from bookmarks;
|
||||||
|
+----+----------+
|
||||||
|
| id | location |
|
||||||
|
|----+----------|
|
||||||
|
+----+----------+
|
||||||
|
```
|
||||||
|
|
||||||
|
You can force Postgres to respect the casing by wrapping the table name in
|
||||||
|
quotes.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> create table "BookMarks" (
|
||||||
|
id integer generated always as identity primary key,
|
||||||
|
location text not null
|
||||||
|
);
|
||||||
|
|
||||||
|
> \d
|
||||||
|
+--------+--------------------+----------+----------+
|
||||||
|
| Schema | Name | Type | Owner |
|
||||||
|
|--------+--------------------+----------+----------|
|
||||||
|
| public | BookMarks | table | postgres |
|
||||||
|
| public | BookMarks_id_seq | sequence | postgres |
|
||||||
|
+--------+--------------------+----------+----------+
|
||||||
|
|
||||||
|
> select * from "BookMarks";
|
||||||
|
+----+----------+
|
||||||
|
| id | location |
|
||||||
|
|----+----------|
|
||||||
|
+----+----------+
|
||||||
|
|
||||||
|
> select * from "bookmarks";
|
||||||
|
relation "bookmarks" does not exist
|
||||||
|
LINE 1: select * from "bookmarks"
|
||||||
|
^
|
||||||
|
|
||||||
|
> select * from BookMarks;
|
||||||
|
relation "bookmarks" does not exist
|
||||||
|
LINE 1: select * from BookMarks
|
||||||
|
^
|
||||||
|
```
|
||||||
|
|
||||||
|
That then means you have to quote your table name anytime you want to refer to
|
||||||
|
it in a query. It's not worth it. It is better to always keep your table names
|
||||||
|
lower-case using snake case.
|
||||||
|
|
||||||
|
[source](https://weiyen.net/articles/avoid-capital-letters-in-postgres-names)
|
||||||
Reference in New Issue
Block a user