Skip to content

Commit e16c130

Browse files
committed
Add Renaming A Sequence as a postgres til
1 parent e44e351 commit e16c130

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_375 TILs and counting..._
10+
_376 TILs and counting..._
1111

1212
---
1313

@@ -195,6 +195,7 @@ _375 TILs and counting..._
195195
- [Max Identifier Length Is 63 Bytes](postgres/max-identifier-length-is-63-bytes.md)
196196
- [pg Prefix Is Reserved For System Schemas](postgres/pg-prefix-is-reserved-for-system-schemas.md)
197197
- [Pretty Print Data Sizes](postgres/pretty-print-data-sizes.md)
198+
- [Renaming A Sequence](postgres/renaming-a-sequence.md)
198199
- [Renaming A Table](postgres/renaming-a-table.md)
199200
- [Restart A Sequence](postgres/restart-a-sequence.md)
200201
- [Restarting Sequences When Truncating Tables](postgres/restarting-sequences-when-truncating-tables.md)

postgres/renaming-a-sequence.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Renaming A Sequence
2+
3+
If a table is created with a `serial` type column, then a sequence is also
4+
created with a name based on the name of the table.
5+
6+
```sql
7+
> \d
8+
List of relations
9+
Schema | Name | Type | Owner
10+
--------+-----------------+----------+------------
11+
public | accounts | table | jbranchaud
12+
public | accounts_id_seq | sequence | jbranchaud
13+
```
14+
15+
In [Renaming A Table](renaming-a-table.md), I showed how a table can be
16+
renamed in PostgreSQL. This will not, however, rename associated sequences.
17+
To maintain naming consistency, you may want to also rename sequences when
18+
renaming tables. This can be done with a query like the following:
19+
20+
```sql
21+
> alter sequence accounts_id_seq rename to users_id_seq;
22+
```
23+
24+
See the [`alter
25+
sequence`](http://www.postgresql.org/docs/current/static/sql-altersequence.html)
26+
docs for more details.

0 commit comments

Comments
 (0)