Skip to content

Commit b9e7d8d

Browse files
committed
Add Print The Query Buffer In psql as a postgres til
1 parent 05a5af4 commit b9e7d8d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-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-
_397 TILs and counting..._
10+
_398 TILs and counting..._
1111

1212
---
1313

@@ -211,6 +211,7 @@ _397 TILs and counting..._
211211
- [Max Identifier Length Is 63 Bytes](postgres/max-identifier-length-is-63-bytes.md)
212212
- [pg Prefix Is Reserved For System Schemas](postgres/pg-prefix-is-reserved-for-system-schemas.md)
213213
- [Pretty Print Data Sizes](postgres/pretty-print-data-sizes.md)
214+
- [Print The Query Buffer In psql](postgres/print-the-query-buffer-in-psql.md)
214215
- [Renaming A Sequence](postgres/renaming-a-sequence.md)
215216
- [Renaming A Table](postgres/renaming-a-table.md)
216217
- [Restart A Sequence](postgres/restart-a-sequence.md)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Print The Query Buffer In psql
2+
3+
I'll often be composing a PostgreSQL query in Vim and decide I want to give
4+
it a try in `psql`. I copy the relevant snippet of SQL to my system buffer
5+
and then paste into `psql`. I'm usually hit with a mess of text like this
6+
though:
7+
8+
```sql
9+
jbranchaud=# create table nullable_fields (
10+
jbranchaud(# id serial primary key,
11+
first varchar,
12+
last varchar
13+
)
14+
id serial primary key,
15+
jbranchaud(# first varchar,
16+
last varchar
17+
)
18+
first varchar,
19+
jbranchaud(# last varchar
20+
)
21+
last varchar
22+
jbranchaud(# )
23+
)
24+
jbranchaud-#
25+
```
26+
27+
Yikes. That's not readable. Fortunately, `psql` provides a command for
28+
printing the current contents of the query buffer. By typing `\p` I'll see a
29+
more readable version of what I just pasted in.
30+
31+
```sql
32+
jbranchaud-# \p
33+
create table nullable_fields (
34+
id serial primary key,
35+
first varchar,
36+
last varchar
37+
)
38+
jbranchaud-#
39+
```
40+
41+
After taking another glance at the snippet of SQL, I decide to complete the
42+
query to create my new table.
43+
44+
```sql
45+
jbranchaud-# ;
46+
CREATE TABLE
47+
```

0 commit comments

Comments
 (0)