mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Default Schema as a postgres til.
This commit is contained in:
@@ -57,6 +57,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
- [Auto Expanded Display](postgres/auto-expanded-display.md)
|
||||
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
||||
- [Count Records By Type](postgres/count-records-by-type.md)
|
||||
- [Default Schema](postgres/default-schema.md)
|
||||
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
||||
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
|
||||
- [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md)
|
||||
|
||||
44
postgres/default-schema.md
Normal file
44
postgres/default-schema.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Default Schema
|
||||
|
||||
Schemas can be used to organize tables within a database. You can see all
|
||||
the schemas your database has like so
|
||||
|
||||
```sql
|
||||
> select schema_name from information_schema.schemata;
|
||||
schema_name
|
||||
--------------------
|
||||
pg_toast
|
||||
pg_temp_1
|
||||
pg_toast_temp_1
|
||||
pg_catalog
|
||||
public
|
||||
information_schema
|
||||
(6 rows)
|
||||
```
|
||||
|
||||
When you create a new table, it will need to be placed under one of these
|
||||
schemas. So if you have a `create table posts (...)`, how does postgres know
|
||||
what schema to put it under?
|
||||
|
||||
Postgres checks your `search_path` for a default.
|
||||
|
||||
```sql
|
||||
> show search_path;
|
||||
search_path
|
||||
-----------------
|
||||
"$user", public
|
||||
(1 row)
|
||||
```
|
||||
|
||||
From our first select statement, we see that there is no schema with my user
|
||||
name, so postgres uses public as the default schema.
|
||||
|
||||
If we set the search path to something that won't resolve to a schema name,
|
||||
postgres will complain
|
||||
|
||||
```sql
|
||||
> set search_path = '$user';
|
||||
SET
|
||||
> create table posts (...);
|
||||
ERROR: no schema has been selected to create in
|
||||
```
|
||||
Reference in New Issue
Block a user