1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>psql</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="app-pgverifybackup.html" title="pg_verifybackup" /><link rel="next" href="app-reindexdb.html" title="reindexdb" /></head><body id="docContent" class="container-fluid col-10"><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center"><span xmlns="http://www.w3.org/1999/xhtml" class="application">psql</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgverifybackup.html" title="pg_verifybackup">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><th width="60%" align="center">PostgreSQL Client Applications</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 14.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="app-reindexdb.html" title="reindexdb">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="APP-PSQL"><div class="titlepage"></div><a id="id-1.9.4.20.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">psql</span></span></h2><p><span class="application">psql</span> —
<span class="productname">PostgreSQL</span> interactive terminal
</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.4.20.4.1"><code class="command">psql</code> [<em class="replaceable"><code>option</code></em>...] [<em class="replaceable"><code>dbname</code></em>
[<em class="replaceable"><code>username</code></em>]]</p></div></div><div class="refsect1" id="id-1.9.4.20.5"><h2>Description</h2><p>
<span class="application">psql</span> is a terminal-based front-end to
<span class="productname">PostgreSQL</span>. It enables you to type in
queries interactively, issue them to
<span class="productname">PostgreSQL</span>, and see the query results.
Alternatively, input can be from a file or from command line
arguments. In addition, <span class="application">psql</span> provides a
number of meta-commands and various shell-like features to
facilitate writing scripts and automating a wide variety of tasks.
</p></div><div class="refsect1" id="R1-APP-PSQL-3"><h2>Options</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-a</code><br /></span><span class="term"><code class="option">--echo-all</code></span></dt><dd><p>
Print all nonempty input lines to standard output as they are read.
(This does not apply to lines read interactively.) This is
equivalent to setting the variable <code class="varname">ECHO</code> to
<code class="literal">all</code>.
</p></dd><dt><span class="term"><code class="option">-A</code><br /></span><span class="term"><code class="option">--no-align</code></span></dt><dd><p>
Switches to unaligned output mode. (The default output mode is
<code class="literal">aligned</code>.) This is equivalent to
<code class="command">\pset format unaligned</code>.
</p></dd><dt><span class="term"><code class="option">-b</code><br /></span><span class="term"><code class="option">--echo-errors</code></span></dt><dd><p>
Print failed SQL commands to standard error output. This is
equivalent to setting the variable <code class="varname">ECHO</code> to
<code class="literal">errors</code>.
</p></dd><dt><span class="term"><code class="option">-c <em class="replaceable"><code>command</code></em></code><br /></span><span class="term"><code class="option">--command=<em class="replaceable"><code>command</code></em></code></span></dt><dd><p>
Specifies that <span class="application">psql</span> is to execute the given
command string, <em class="replaceable"><code>command</code></em>.
This option can be repeated and combined in any order with
the <code class="option">-f</code> option. When either <code class="option">-c</code>
or <code class="option">-f</code> is specified, <span class="application">psql</span>
does not read commands from standard input; instead it terminates
after processing all the <code class="option">-c</code> and <code class="option">-f</code>
options in sequence.
</p><p>
<em class="replaceable"><code>command</code></em> must be either
a command string that is completely parsable by the server (i.e.,
it contains no <span class="application">psql</span>-specific features),
or a single backslash command. Thus you cannot mix
<acronym class="acronym">SQL</acronym> and <span class="application">psql</span>
meta-commands within a <code class="option">-c</code> option. To achieve that,
you could use repeated <code class="option">-c</code> options or pipe the string
into <span class="application">psql</span>, for example:
</p><pre class="programlisting">
psql -c '\x' -c 'SELECT * FROM foo;'
</pre><p>
or
</p><pre class="programlisting">
echo '\x \\ SELECT * FROM foo;' | psql
</pre><p>
(<code class="literal">\\</code> is the separator meta-command.)
</p><p>
Each <acronym class="acronym">SQL</acronym> command string passed
to <code class="option">-c</code> is sent to the server as a single request.
Because of this, the server executes it as a single transaction even
if the string contains multiple <acronym class="acronym">SQL</acronym> commands,
unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
commands included in the string to divide it into multiple
transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="53.2.2.1. Multiple Statements in a Simple Query">Section 53.2.2.1</a>
for more details about how the server handles multi-query strings.)
Also, <span class="application">psql</span> only prints the
result of the last <acronym class="acronym">SQL</acronym> command in the string.
This is different from the behavior when the same string is read from
a file or fed to <span class="application">psql</span>'s standard input,
because then <span class="application">psql</span> sends
each <acronym class="acronym">SQL</acronym> command separately.
</p><p>
Because of this behavior, putting more than one SQL command in a
single <code class="option">-c</code> string often has unexpected results.
It's better to use repeated <code class="option">-c</code> commands or feed
multiple commands to <span class="application">psql</span>'s standard input,
either using <span class="application">echo</span> as illustrated above, or
via a shell here-document, for example:
</p><pre class="programlisting">
psql <<EOF
\x
SELECT * FROM foo;
EOF
</pre></dd><dt><span class="term"><code class="option">--csv</code></span></dt><dd><p>
Switches to <acronym class="acronym">CSV</acronym> (Comma-Separated Values) output
mode. This is equivalent to <code class="command">\pset format csv</code>.
</p></dd><dt><span class="term"><code class="option">-d <em class="replaceable"><code>dbname</code></em></code><br /></span><span class="term"><code class="option">--dbname=<em class="replaceable"><code>dbname</code></em></code></span></dt><dd><p>
Specifies the name of the database to connect to. This is
equivalent to specifying <em class="replaceable"><code>dbname</code></em> as the first non-option
argument on the command line. The <em class="replaceable"><code>dbname</code></em>
can be a <a class="link" href="libpq-connect.html#LIBPQ-CONNSTRING" title="34.1.1. Connection Strings">connection string</a>.
If so, connection string parameters will override any conflicting
command line options.
</p></dd><dt><span class="term"><code class="option">-e</code><br /></span><span class="term"><code class="option">--echo-queries</code></span></dt><dd><p>
Copy all SQL commands sent to the server to standard output as well.
This is equivalent
to setting the variable <code class="varname">ECHO</code> to
<code class="literal">queries</code>.
</p></dd><dt><span class="term"><code class="option">-E</code><br /></span><span class="term"><code class="option">--echo-hidden</code></span></dt><dd><p>
Echo the actual queries generated by <code class="command">\d</code> and other backslash
commands. You can use this to study <span class="application">psql</span>'s
internal operations. This is equivalent to
setting the variable <code class="varname">ECHO_HIDDEN</code> to <code class="literal">on</code>.
</p></dd><dt><span class="term"><code class="option">-f <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--file=<em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
Read commands from the
file <em class="replaceable"><code>filename</code></em>,
rather than standard input.
This option can be repeated and combined in any order with
the <code class="option">-c</code> option. When either <code class="option">-c</code>
or <code class="option">-f</code> is specified, <span class="application">psql</span>
does not read commands from standard input; instead it terminates
after processing all the <code class="option">-c</code> and <code class="option">-f</code>
options in sequence.
Except for that, this option is largely equivalent to the
meta-command <code class="command">\i</code>.
</p><p>
If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
(hyphen), then standard input is read until an EOF indication
or <code class="command">\q</code> meta-command. This can be used to intersperse
interactive input with input from files. Note however that Readline
is not used in this case (much as if <code class="option">-n</code> had been
specified).
</p><p>
Using this option is subtly different from writing <code class="literal">psql
< <em class="replaceable"><code>filename</code></em></code>. In general,
both will do what you expect, but using <code class="literal">-f</code>
enables some nice features such as error messages with line
numbers. There is also a slight chance that using this option will
reduce the start-up overhead. On the other hand, the variant using
the shell's input redirection is (in theory) guaranteed to yield
exactly the same output you would have received had you entered
everything by hand.
</p></dd><dt><span class="term"><code class="option">-F <em class="replaceable"><code>separator</code></em></code><br /></span><span class="term"><code class="option">--field-separator=<em class="replaceable"><code>separator</code></em></code></span></dt><dd><p>
Use <em class="replaceable"><code>separator</code></em> as the
field separator for unaligned output. This is equivalent to
<code class="command">\pset fieldsep</code> or <code class="command">\f</code>.
</p></dd><dt><span class="term"><code class="option">-h <em class="replaceable"><code>hostname</code></em></code><br /></span><span class="term"><code class="option">--host=<em class="replaceable"><code>hostname</code></em></code></span></dt><dd><p>
Specifies the host name of the machine on which the
server is running. If the value begins
with a slash, it is used as the directory for the Unix-domain
socket.
</p></dd><dt><span class="term"><code class="option">-H</code><br /></span><span class="term"><code class="option">--html</code></span></dt><dd><p>
Switches to <acronym class="acronym">HTML</acronym> output mode. This is
equivalent to <code class="command">\pset format html</code> or the
<code class="command">\H</code> command.
</p></dd><dt><span class="term"><code class="option">-l</code><br /></span><span class="term"><code class="option">--list</code></span></dt><dd><p>
List all available databases, then exit. Other non-connection
options are ignored. This is similar to the meta-command
<code class="command">\list</code>.
</p><p>
When this option is used, <span class="application">psql</span> will connect
to the database <code class="literal">postgres</code>, unless a different database
is named on the command line (option <code class="option">-d</code> or non-option
argument, possibly via a service entry, but not via an environment
variable).
</p></dd><dt><span class="term"><code class="option">-L <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--log-file=<em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
Write all query output into file <em class="replaceable"><code>filename</code></em>, in addition to the
normal output destination.
</p></dd><dt><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-readline</code></span></dt><dd><p>
Do not use <span class="application">Readline</span> for line editing and do
not use the command history.
This can be useful to turn off tab expansion when cutting and pasting.
</p></dd><dt><span class="term"><code class="option">-o <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--output=<em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
Put all query output into file <em class="replaceable"><code>filename</code></em>. This is equivalent to
the command <code class="command">\o</code>.
</p></dd><dt><span class="term"><code class="option">-p <em class="replaceable"><code>port</code></em></code><br /></span><span class="term"><code class="option">--port=<em class="replaceable"><code>port</code></em></code></span></dt><dd><p>
Specifies the TCP port or the local Unix-domain
socket file extension on which the server is listening for
connections. Defaults to the value of the <code class="envar">PGPORT</code>
environment variable or, if not set, to the port specified at
compile time, usually 5432.
</p></dd><dt><span class="term"><code class="option">-P <em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--pset=<em class="replaceable"><code>assignment</code></em></code></span></dt><dd><p>
Specifies printing options, in the style of
<code class="command">\pset</code>. Note that here you
have to separate name and value with an equal sign instead of a
space. For example, to set the output format to <span class="application">LaTeX</span>, you could write
<code class="literal">-P format=latex</code>.
</p></dd><dt><span class="term"><code class="option">-q</code><br /></span><span class="term"><code class="option">--quiet</code></span></dt><dd><p>
Specifies that <span class="application">psql</span> should do its work
quietly. By default, it prints welcome messages and various
informational output. If this option is used, none of this
happens. This is useful with the <code class="option">-c</code> option.
This is equivalent to setting the variable <code class="varname">QUIET</code>
to <code class="literal">on</code>.
</p></dd><dt><span class="term"><code class="option">-R <em class="replaceable"><code>separator</code></em></code><br /></span><span class="term"><code class="option">--record-separator=<em class="replaceable"><code>separator</code></em></code></span></dt><dd><p>
Use <em class="replaceable"><code>separator</code></em> as the
record separator for unaligned output. This is equivalent to
<code class="command">\pset recordsep</code>.
</p></dd><dt><span class="term"><code class="option">-s</code><br /></span><span class="term"><code class="option">--single-step</code></span></dt><dd><p>
Run in single-step mode. That means the user is prompted before
each command is sent to the server, with the option to cancel
execution as well. Use this to debug scripts.
</p></dd><dt><span class="term"><code class="option">-S</code><br /></span><span class="term"><code class="option">--single-line</code></span></dt><dd><p>
Runs in single-line mode where a newline terminates an SQL command, as a
semicolon does.
</p><div class="note"><h3 class="title">Note</h3><p>
This mode is provided for those who insist on it, but you are not
necessarily encouraged to use it. In particular, if you mix
<acronym class="acronym">SQL</acronym> and meta-commands on a line the order of
execution might not always be clear to the inexperienced user.
</p></div></dd><dt><span class="term"><code class="option">-t</code><br /></span><span class="term"><code class="option">--tuples-only</code></span></dt><dd><p>
Turn off printing of column names and result row count footers,
etc. This is equivalent to <code class="command">\t</code> or
<code class="command">\pset tuples_only</code>.
</p></dd><dt><span class="term"><code class="option">-T <em class="replaceable"><code>table_options</code></em></code><br /></span><span class="term"><code class="option">--table-attr=<em class="replaceable"><code>table_options</code></em></code></span></dt><dd><p>
Specifies options to be placed within the
<acronym class="acronym">HTML</acronym> <code class="sgmltag-element">table</code> tag. See
<code class="command">\pset tableattr</code> for details.
</p></dd><dt><span class="term"><code class="option">-U <em class="replaceable"><code>username</code></em></code><br /></span><span class="term"><code class="option">--username=<em class="replaceable"><code>username</code></em></code></span></dt><dd><p>
Connect to the database as the user <em class="replaceable"><code>username</code></em> instead of the default.
(You must have permission to do so, of course.)
</p></dd><dt><span class="term"><code class="option">-v <em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--set=<em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--variable=<em class="replaceable"><code>assignment</code></em></code></span></dt><dd><p>
Perform a variable assignment, like the <code class="command">\set</code>
meta-command. Note that you must separate name and value, if
any, by an equal sign on the command line. To unset a variable,
leave off the equal sign. To set a variable with an empty value,
use the equal sign but leave off the value. These assignments are
done during command line processing, so variables that reflect
connection state will get overwritten later.
</p></dd><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>
Print the <span class="application">psql</span> version and exit.
</p></dd><dt><span class="term"><code class="option">-w</code><br /></span><span class="term"><code class="option">--no-password</code></span></dt><dd><p>
Never issue a password prompt. If the server requires password
authentication and a password is not available from other sources
such as a <code class="filename">.pgpass</code> file, the connection
attempt will fail. This option can be useful in batch jobs and
scripts where no user is present to enter a password.
</p><p>
Note that this option will remain set for the entire session,
and so it affects uses of the meta-command
<code class="command">\connect</code> as well as the initial connection attempt.
</p></dd><dt><span class="term"><code class="option">-W</code><br /></span><span class="term"><code class="option">--password</code></span></dt><dd><p>
Force <span class="application">psql</span> to prompt for a
password before connecting to a database, even if the password will
not be used.
</p><p>
If the server requires password authentication and a password is not
available from other sources such as a <code class="filename">.pgpass</code>
file, <span class="application">psql</span> will prompt for a
password in any case. However, <span class="application">psql</span>
will waste a connection attempt finding out that the server wants a
password. In some cases it is worth typing <code class="option">-W</code> to avoid
the extra connection attempt.
</p><p>
Note that this option will remain set for the entire session,
and so it affects uses of the meta-command
<code class="command">\connect</code> as well as the initial connection attempt.
</p></dd><dt><span class="term"><code class="option">-x</code><br /></span><span class="term"><code class="option">--expanded</code></span></dt><dd><p>
Turn on the expanded table formatting mode. This is equivalent to
<code class="command">\x</code> or <code class="command">\pset expanded</code>.
</p></dd><dt><span class="term"><code class="option">-X,</code><br /></span><span class="term"><code class="option">--no-psqlrc</code></span></dt><dd><p>
Do not read the start-up file (neither the system-wide
<code class="filename">psqlrc</code> file nor the user's
<code class="filename">~/.psqlrc</code> file).
</p></dd><dt><span class="term"><code class="option">-z</code><br /></span><span class="term"><code class="option">--field-separator-zero</code></span></dt><dd><p>
Set the field separator for unaligned output to a zero byte. This is
equivalent to <code class="command">\pset fieldsep_zero</code>.
</p></dd><dt><span class="term"><code class="option">-0</code><br /></span><span class="term"><code class="option">--record-separator-zero</code></span></dt><dd><p>
Set the record separator for unaligned output to a zero byte. This is
useful for interfacing, for example, with <code class="literal">xargs -0</code>.
This is equivalent to <code class="command">\pset recordsep_zero</code>.
</p></dd><dt><span class="term"><code class="option">-1</code><br /></span><span class="term"><code class="option">--single-transaction</code></span></dt><dd><p>
This option can only be used in combination with one or more
<code class="option">-c</code> and/or <code class="option">-f</code> options. It causes
<span class="application">psql</span> to issue a <code class="command">BEGIN</code> command
before the first such option and a <code class="command">COMMIT</code> command after
the last one, thereby wrapping all the commands into a single
transaction. This ensures that either all the commands complete
successfully, or no changes are applied.
</p><p>
If the commands themselves
contain <code class="command">BEGIN</code>, <code class="command">COMMIT</code>,
or <code class="command">ROLLBACK</code>, this option will not have the desired
effects. Also, if an individual command cannot be executed inside a
transaction block, specifying this option will cause the whole
transaction to fail.
</p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help[=<em class="replaceable"><code>topic</code></em>]</code></span></dt><dd><p>
Show help about <span class="application">psql</span> and exit. The optional
<em class="replaceable"><code>topic</code></em> parameter (defaulting
to <code class="literal">options</code>) selects which part of <span class="application">psql</span> is
explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
backslash commands; <code class="literal">options</code> describes the command-line
options that can be passed to <span class="application">psql</span>;
and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
variables.
</p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.20.7"><h2>Exit Status</h2><p>
<span class="application">psql</span> returns 0 to the shell if it
finished normally, 1 if a fatal error of its own occurs (e.g., out of memory,
file not found), 2 if the connection to the server went bad
and the session was not interactive, and 3 if an error occurred in a
script and the variable <code class="varname">ON_ERROR_STOP</code> was set.
</p></div><div class="refsect1" id="id-1.9.4.20.8"><h2>Usage</h2><div class="refsect2" id="R2-APP-PSQL-CONNECTING"><h3>Connecting to a Database</h3><p>
<span class="application">psql</span> is a regular
<span class="productname">PostgreSQL</span> client application. In order
to connect to a database you need to know the name of your target
database, the host name and port number of the server, and what user
name you want to connect as. <span class="application">psql</span> can be
told about those parameters via command line options, namely
<code class="option">-d</code>, <code class="option">-h</code>, <code class="option">-p</code>, and
<code class="option">-U</code> respectively. If an argument is found that does
not belong to any option it will be interpreted as the database name
(or the user name, if the database name is already given). Not all
of these options are required; there are useful defaults. If you omit the host
name, <span class="application">psql</span> will connect via a Unix-domain socket
to a server on the local host, or via TCP/IP to <code class="literal">localhost</code> on
machines that don't have Unix-domain sockets. The default port number is
determined at compile time.
Since the database server uses the same default, you will not have
to specify the port in most cases. The default user name is your
operating-system user name, as is the default database name.
Note that you cannot
just connect to any database under any user name. Your database
administrator should have informed you about your access rights.
</p><p>
When the defaults aren't quite right, you can save yourself
some typing by setting the environment variables
<code class="envar">PGDATABASE</code>, <code class="envar">PGHOST</code>,
<code class="envar">PGPORT</code> and/or <code class="envar">PGUSER</code> to appropriate
values. (For additional environment variables, see <a class="xref" href="libpq-envars.html" title="34.15. Environment Variables">Section 34.15</a>.) It is also convenient to have a
<code class="filename">~/.pgpass</code> file to avoid regularly having to type in
passwords. See <a class="xref" href="libpq-pgpass.html" title="34.16. The Password File">Section 34.16</a> for more information.
</p><p>
An alternative way to specify connection parameters is in a
<em class="parameter"><code>conninfo</code></em> string or
a <acronym class="acronym">URI</acronym>, which is used instead of a database
name. This mechanism give you very wide control over the
connection. For example:
</p><pre class="programlisting">
$ <strong class="userinput"><code>psql "service=myservice sslmode=require"</code></strong>
$ <strong class="userinput"><code>psql postgresql://dbmaster:5433/mydb?sslmode=require</code></strong>
</pre><p>
This way you can also use <acronym class="acronym">LDAP</acronym> for connection
parameter lookup as described in <a class="xref" href="libpq-ldap.html" title="34.18. LDAP Lookup of Connection Parameters">Section 34.18</a>.
See <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="34.1.2. Parameter Key Words">Section 34.1.2</a> for more information on all the
available connection options.
</p><p>
If the connection could not be made for any reason (e.g., insufficient
privileges, server is not running on the targeted host, etc.),
<span class="application">psql</span> will return an error and terminate.
</p><p>
If both standard input and standard output are a
terminal, then <span class="application">psql</span> sets the client
encoding to <span class="quote">“<span class="quote">auto</span>”</span>, which will detect the
appropriate client encoding from the locale settings
(<code class="envar">LC_CTYPE</code> environment variable on Unix systems).
If this doesn't work out as expected, the client encoding can be
overridden using the environment
variable <code class="envar">PGCLIENTENCODING</code>.
</p></div><div class="refsect2" id="R2-APP-PSQL-4"><h3>Entering SQL Commands</h3><p>
In normal operation, <span class="application">psql</span> provides a
prompt with the name of the database to which
<span class="application">psql</span> is currently connected, followed by
the string <code class="literal">=></code>. For example:
</p><pre class="programlisting">
$ <strong class="userinput"><code>psql testdb</code></strong>
psql (14.5)
Type "help" for help.
testdb=>
</pre><p>
</p><p>
At the prompt, the user can type in <acronym class="acronym">SQL</acronym> commands.
Ordinarily, input lines are sent to the server when a
command-terminating semicolon is reached. An end of line does not
terminate a command. Thus commands can be spread over several lines for
clarity. If the command was sent and executed without error, the results
of the command are displayed on the screen.
</p><p>
If untrusted users have access to a database that has not adopted a
<a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.9.6. Usage Patterns">secure schema usage pattern</a>,
begin your session by removing publicly-writable schemas
from <code class="varname">search_path</code>. One can
add <code class="literal">options=-csearch_path=</code> to the connection string or
issue <code class="literal">SELECT pg_catalog.set_config('search_path', '',
false)</code> before other SQL commands. This consideration is not
specific to <span class="application">psql</span>; it applies to every interface
for executing arbitrary SQL commands.
</p><p>
Whenever a command is executed, <span class="application">psql</span> also polls
for asynchronous notification events generated by
<a class="link" href="sql-listen.html" title="LISTEN"><code class="command">LISTEN</code></a> and
<a class="link" href="sql-notify.html" title="NOTIFY"><code class="command">NOTIFY</code></a>.
</p><p>
While C-style block comments are passed to the server for
processing and removal, SQL-standard comments are removed by
<span class="application">psql</span>.
</p></div><div class="refsect2" id="APP-PSQL-META-COMMANDS"><h3>Meta-Commands</h3><p>
Anything you enter in <span class="application">psql</span> that begins
with an unquoted backslash is a <span class="application">psql</span>
meta-command that is processed by <span class="application">psql</span>
itself. These commands make
<span class="application">psql</span> more useful for administration or
scripting. Meta-commands are often called slash or backslash commands.
</p><p>
The format of a <span class="application">psql</span> command is the backslash,
followed immediately by a command verb, then any arguments. The arguments
are separated from the command verb and each other by any number of
whitespace characters.
</p><p>
To include whitespace in an argument you can quote it with
single quotes. To include a single quote in an argument,
write two single quotes within single-quoted text.
Anything contained in single quotes is
furthermore subject to C-like substitutions for
<code class="literal">\n</code> (new line), <code class="literal">\t</code> (tab),
<code class="literal">\b</code> (backspace), <code class="literal">\r</code> (carriage return),
<code class="literal">\f</code> (form feed),
<code class="literal">\</code><em class="replaceable"><code>digits</code></em> (octal), and
<code class="literal">\x</code><em class="replaceable"><code>digits</code></em> (hexadecimal).
A backslash preceding any other character within single-quoted text
quotes that single character, whatever it is.
</p><p>
If an unquoted colon (<code class="literal">:</code>) followed by a
<span class="application">psql</span> variable name appears within an argument, it is
replaced by the variable's value, as described in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation">SQL Interpolation</a> below.
The forms <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> and
<code class="literal">:"<em class="replaceable"><code>variable_name</code></em>"</code> described there
work as well.
The <code class="literal">:{?<em class="replaceable"><code>variable_name</code></em>}</code> syntax allows
testing whether a variable is defined. It is substituted by
TRUE or FALSE.
Escaping the colon with a backslash protects it from substitution.
</p><p>
Within an argument, text that is enclosed in backquotes
(<code class="literal">`</code>) is taken as a command line that is passed to the
shell. The output of the command (with any trailing newline removed)
replaces the backquoted text. Within the text enclosed in backquotes,
no special quoting or other processing occurs, except that appearances
of <code class="literal">:<em class="replaceable"><code>variable_name</code></em></code> where
<em class="replaceable"><code>variable_name</code></em> is a <span class="application">psql</span> variable name
are replaced by the variable's value. Also, appearances of
<code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> are replaced by the
variable's value suitably quoted to become a single shell command
argument. (The latter form is almost always preferable, unless you are
very sure of what is in the variable.) Because carriage return and line
feed characters cannot be safely quoted on all platforms, the
<code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> form prints an
error message and does not substitute the variable value when such
characters appear in the value.
</p><p>
Some commands take an <acronym class="acronym">SQL</acronym> identifier (such as a
table name) as argument. These arguments follow the syntax rules
of <acronym class="acronym">SQL</acronym>: Unquoted letters are forced to
lowercase, while double quotes (<code class="literal">"</code>) protect letters
from case conversion and allow incorporation of whitespace into
the identifier. Within double quotes, paired double quotes reduce
to a single double quote in the resulting name. For example,
<code class="literal">FOO"BAR"BAZ</code> is interpreted as <code class="literal">fooBARbaz</code>,
and <code class="literal">"A weird"" name"</code> becomes <code class="literal">A weird"
name</code>.
</p><p>
Parsing for arguments stops at the end of the line, or when another
unquoted backslash is found. An unquoted backslash
is taken as the beginning of a new meta-command. The special
sequence <code class="literal">\\</code> (two backslashes) marks the end of
arguments and continues parsing <acronym class="acronym">SQL</acronym> commands, if
any. That way <acronym class="acronym">SQL</acronym> and
<span class="application">psql</span> commands can be freely mixed on a
line. But in any case, the arguments of a meta-command cannot
continue beyond the end of the line.
</p><p>
Many of the meta-commands act on the <em class="firstterm">current query buffer</em>.
This is simply a buffer holding whatever SQL command text has been typed
but not yet sent to the server for execution. This will include previous
input lines as well as any text appearing before the meta-command on the
same line.
</p><p>
The following meta-commands are defined:
</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">\a</code></span></dt><dd><p>
If the current table output format is unaligned, it is switched to aligned.
If it is not unaligned, it is set to unaligned. This command is
kept for backwards compatibility. See <code class="command">\pset</code> for a
more general solution.
</p></dd><dt><span class="term"><code class="literal">\c</code> or <code class="literal">\connect [ -reuse-previous=<em class="replaceable"><code>on|off</code></em> ] [ <em class="replaceable"><code>dbname</code></em> [ <em class="replaceable"><code>username</code></em> ] [ <em class="replaceable"><code>host</code></em> ] [ <em class="replaceable"><code>port</code></em> ] | <em class="replaceable"><code>conninfo</code></em> ]</code></span></dt><dd><p>
Establishes a new connection to a <span class="productname">PostgreSQL</span>
server. The connection parameters to use can be specified either
using a positional syntax (one or more of database name, user,
host, and port), or using a <em class="replaceable"><code>conninfo</code></em>
connection string as detailed in
<a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="34.1.1. Connection Strings">Section 34.1.1</a>. If no arguments are given, a
new connection is made using the same parameters as before.
</p><p>
Specifying any
of <em class="replaceable"><code>dbname</code></em>,
<em class="replaceable"><code>username</code></em>,
<em class="replaceable"><code>host</code></em> or
<em class="replaceable"><code>port</code></em>
as <code class="literal">-</code> is equivalent to omitting that parameter.
</p><p>
The new connection can re-use connection parameters from the previous
connection; not only database name, user, host, and port, but other
settings such as <em class="replaceable"><code>sslmode</code></em>. By default,
parameters are re-used in the positional syntax, but not when
a <em class="replaceable"><code>conninfo</code></em> string is given. Passing a
first argument of <code class="literal">-reuse-previous=on</code>
or <code class="literal">-reuse-previous=off</code> overrides that default. If
parameters are re-used, then any parameter not explicitly specified as
a positional parameter or in the <em class="replaceable"><code>conninfo</code></em>
string is taken from the existing connection's parameters. An
exception is that if the <em class="replaceable"><code>host</code></em> setting
is changed from its previous value using the positional syntax,
any <em class="replaceable"><code>hostaddr</code></em> setting present in the
existing connection's parameters is dropped.
Also, any password used for the existing connection will be re-used
only if the user, host, and port settings are not changed.
When the command neither specifies nor reuses a particular parameter,
the <span class="application">libpq</span> default is used.
</p><p>
If the new connection is successfully made, the previous
connection is closed.
If the connection attempt fails (wrong user name, access
denied, etc.), the previous connection will be kept if
<span class="application">psql</span> is in interactive mode. But when
executing a non-interactive script, the old connection is closed
and an error is reported. That may or may not terminate the
script; if it does not, all database-accessing commands will fail
until another <code class="literal">\connect</code> command is successfully
executed. This distinction was chosen as
a user convenience against typos on the one hand, and a safety
mechanism that scripts are not accidentally acting on the
wrong database on the other hand.
Note that whenever a <code class="literal">\connect</code> command attempts
to re-use parameters, the values re-used are those of the last
successful connection, not of any failed attempts made subsequently.
However, in the case of a
non-interactive <code class="literal">\connect</code> failure, no parameters
are allowed to be re-used later, since the script would likely be
expecting the values from the failed <code class="literal">\connect</code>
to be re-used.
</p><p>
Examples:
</p><pre class="programlisting">
=> \c mydb myuser host.dom 6432
=> \c service=foo
=> \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
=> \c -reuse-previous=on sslmode=require -- changes only sslmode
=> \c postgresql://tom@localhost/mydb?application_name=myapp
</pre></dd><dt><span class="term"><code class="literal">\C [ <em class="replaceable"><code>title</code></em> ]</code></span></dt><dd><p>
Sets the title of any tables being printed as the result of a
query or unset any such title. This command is equivalent to
<code class="literal">\pset title <em class="replaceable"><code>title</code></em></code>. (The name of
this command derives from <span class="quote">“<span class="quote">caption</span>”</span>, as it was
previously only used to set the caption in an
<acronym class="acronym">HTML</acronym> table.)
</p></dd><dt><span class="term"><code class="literal">\cd [ <em class="replaceable"><code>directory</code></em> ]</code></span></dt><dd><p>
Changes the current working directory to
<em class="replaceable"><code>directory</code></em>. Without argument, changes
to the current user's home directory.
</p><div class="tip"><h3 class="title">Tip</h3><p>
To print your current working directory, use <code class="literal">\! pwd</code>.
</p></div></dd><dt><span class="term"><code class="literal">\conninfo</code></span></dt><dd><p>
Outputs information about the current database connection.
</p></dd><dt id="APP-PSQL-META-COMMANDS-COPY"><span class="term"><code class="literal">\copy { <em class="replaceable"><code>table</code></em> [ ( <em class="replaceable"><code>column_list</code></em> ) ] }
<code class="literal">from</code>
{ <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdin | pstdin }
[ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]
[ where <em class="replaceable"><code>condition</code></em> ]</code><br /></span><span class="term"><code class="literal">\copy { <em class="replaceable"><code>table</code></em> [ ( <em class="replaceable"><code>column_list</code></em> ) ] | ( <em class="replaceable"><code>query</code></em> ) }
<code class="literal">to</code>
{ <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdout | pstdout }
[ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]</code></span></dt><dd><p>
Performs a frontend (client) copy. This is an operation that
runs an <acronym class="acronym">SQL</acronym> <a class="link" href="sql-copy.html" title="COPY"><code class="command">COPY</code></a>
command, but instead of the server
reading or writing the specified file,
<span class="application">psql</span> reads or writes the file and
routes the data between the server and the local file system.
This means that file accessibility and privileges are those of
the local user, not the server, and no SQL superuser
privileges are required.
</p><p>
When <code class="literal">program</code> is specified,
<em class="replaceable"><code>command</code></em> is
executed by <span class="application">psql</span> and the data passed from
or to <em class="replaceable"><code>command</code></em> is
routed between the server and the client.
Again, the execution privileges are those of
the local user, not the server, and no SQL superuser
privileges are required.
</p><p>
For <code class="literal">\copy ... from stdin</code>, data rows are read from the same
source that issued the command, continuing until <code class="literal">\.</code>
is read or the stream reaches <acronym class="acronym">EOF</acronym>. This option is useful
for populating tables in-line within an SQL script file.
For <code class="literal">\copy ... to stdout</code>, output is sent to the same place
as <span class="application">psql</span> command output, and
the <code class="literal">COPY <em class="replaceable"><code>count</code></em></code> command status is
not printed (since it might be confused with a data row).
To read/write <span class="application">psql</span>'s standard input or
output regardless of the current command source or <code class="literal">\o</code>
option, write <code class="literal">from pstdin</code> or <code class="literal">to pstdout</code>.
</p><p>
The syntax of this command is similar to that of the
<acronym class="acronym">SQL</acronym> <a class="link" href="sql-copy.html" title="COPY"><code class="command">COPY</code></a>
command. All options other than the data source/destination are
as specified for <code class="command">COPY</code>.
Because of this, special parsing rules apply to the <code class="command">\copy</code>
meta-command. Unlike most other meta-commands, the entire remainder
of the line is always taken to be the arguments of <code class="command">\copy</code>,
and neither variable interpolation nor backquote expansion are
performed in the arguments.
</p><div class="tip"><h3 class="title">Tip</h3><p>
Another way to obtain the same result as <code class="literal">\copy
... to</code> is to use the <acronym class="acronym">SQL</acronym> <code class="literal">COPY
... TO STDOUT</code> command and terminate it
with <code class="literal">\g <em class="replaceable"><code>filename</code></em></code>
or <code class="literal">\g |<em class="replaceable"><code>program</code></em></code>.
Unlike <code class="literal">\copy</code>, this method allows the command to
span multiple lines; also, variable interpolation and backquote
expansion can be used.
</p></div><div class="tip"><h3 class="title">Tip</h3><p>
These operations are not as efficient as the <acronym class="acronym">SQL</acronym>
<code class="command">COPY</code> command with a file or program data source or
destination, because all data must pass through the client/server
connection. For large amounts of data the <acronym class="acronym">SQL</acronym>
command might be preferable.
</p></div></dd><dt><span class="term"><code class="literal">\copyright</code></span></dt><dd><p>
Shows the copyright and distribution terms of
<span class="productname">PostgreSQL</span>.
</p></dd><dt id="APP-PSQL-META-COMMANDS-CROSSTABVIEW"><span class="term"><code class="literal">\crosstabview [
<em class="replaceable"><code>colV</code></em>
[ <em class="replaceable"><code>colH</code></em>
[ <em class="replaceable"><code>colD</code></em>
[ <em class="replaceable"><code>sortcolH</code></em>
] ] ] ] </code></span></dt><dd><p>
Executes the current query buffer (like <code class="literal">\g</code>) and
shows the results in a crosstab grid.
The query must return at least three columns.
The output column identified by <em class="replaceable"><code>colV</code></em>
becomes a vertical header and the output column identified by
<em class="replaceable"><code>colH</code></em>
becomes a horizontal header.
<em class="replaceable"><code>colD</code></em> identifies
the output column to display within the grid.
<em class="replaceable"><code>sortcolH</code></em> identifies
an optional sort column for the horizontal header.
</p><p>
Each column specification can be a column number (starting at 1) or
a column name. The usual SQL case folding and quoting rules apply to
column names. If omitted,
<em class="replaceable"><code>colV</code></em> is taken as column 1
and <em class="replaceable"><code>colH</code></em> as column 2.
<em class="replaceable"><code>colH</code></em> must differ from
<em class="replaceable"><code>colV</code></em>.
If <em class="replaceable"><code>colD</code></em> is not
specified, then there must be exactly three columns in the query
result, and the column that is neither
<em class="replaceable"><code>colV</code></em> nor
<em class="replaceable"><code>colH</code></em>
is taken to be <em class="replaceable"><code>colD</code></em>.
</p><p>
The vertical header, displayed as the leftmost column, contains the
values found in column <em class="replaceable"><code>colV</code></em>, in the
same order as in the query results, but with duplicates removed.
</p><p>
The horizontal header, displayed as the first row, contains the values
found in column <em class="replaceable"><code>colH</code></em>,
with duplicates removed. By default, these appear in the same order
as in the query results. But if the
optional <em class="replaceable"><code>sortcolH</code></em> argument is given,
it identifies a column whose values must be integer numbers, and the
values from <em class="replaceable"><code>colH</code></em> will
appear in the horizontal header sorted according to the
corresponding <em class="replaceable"><code>sortcolH</code></em> values.
</p><p>
Inside the crosstab grid, for each distinct value <code class="literal">x</code>
of <em class="replaceable"><code>colH</code></em> and each distinct
value <code class="literal">y</code>
of <em class="replaceable"><code>colV</code></em>, the cell located
at the intersection <code class="literal">(x,y)</code> contains the value of
the <code class="literal">colD</code> column in the query result row for which
the value of <em class="replaceable"><code>colH</code></em>
is <code class="literal">x</code> and the value
of <em class="replaceable"><code>colV</code></em>
is <code class="literal">y</code>. If there is no such row, the cell is empty. If
there are multiple such rows, an error is reported.
</p></dd><dt><span class="term"><code class="literal">\d[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
For each relation (table, view, materialized view, index, sequence,
or foreign table)
or composite type matching the
<em class="replaceable"><code>pattern</code></em>, show all
columns, their types, the tablespace (if not the default) and any
special attributes such as <code class="literal">NOT NULL</code> or defaults.
Associated indexes, constraints, rules, and triggers are
also shown. For foreign tables, the associated foreign
server is shown as well.
(<span class="quote">“<span class="quote">Matching the pattern</span>”</span> is defined in
<a class="xref" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns">Patterns</a> below.)
</p><p>
For some types of relation, <code class="literal">\d</code> shows additional information
for each column: column values for sequences, indexed expressions for
indexes, and foreign data wrapper options for foreign tables.
</p><p>
The command form <code class="literal">\d+</code> is identical, except that
more information is displayed: any comments associated with the
columns of the table are shown, as is the presence of OIDs in the
table, the view definition if the relation is a view, a non-default
<a class="link" href="sql-altertable.html#SQL-ALTERTABLE-REPLICA-IDENTITY">replica
identity</a> setting and the
<a class="link" href="sql-create-access-method.html" title="CREATE ACCESS METHOD">access method</a> name
if the relation has an access method.
</p><p>
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
</p><div class="note"><h3 class="title">Note</h3><p>
If <code class="command">\d</code> is used without a
<em class="replaceable"><code>pattern</code></em> argument, it is
equivalent to <code class="command">\dtvmsE</code> which will show a list of
all visible tables, views, materialized views, sequences and
foreign tables.
This is purely a convenience measure.
</p></div></dd><dt><span class="term"><code class="literal">\da[S] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists aggregate functions, together with their
return type and the data types they operate on. If <em class="replaceable"><code>pattern</code></em>
is specified, only aggregates whose names match the pattern are shown.
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
</p></dd><dt><span class="term"><code class="literal">\dA[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists access methods. If <em class="replaceable"><code>pattern</code></em> is specified, only access
methods whose names match the pattern are shown. If
<code class="literal">+</code> is appended to the command name, each access
method is listed with its associated handler function and description.
</p></dd><dt><span class="term">
<code class="literal">\dAc[+]
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>input-type-pattern</code></em></a>]]
</code>
</span></dt><dd><p>
Lists operator classes
(see <a class="xref" href="xindex.html#XINDEX-OPCLASS" title="38.16.1. Index Methods and Operator Classes">Section 38.16.1</a>).
If <em class="replaceable"><code>access-method-pattern</code></em>
is specified, only operator classes associated with access methods whose
names match that pattern are listed.
If <em class="replaceable"><code>input-type-pattern</code></em>
is specified, only operator classes associated with input types whose
names match that pattern are listed.
If <code class="literal">+</code> is appended to the command name, each operator
class is listed with its associated operator family and owner.
</p></dd><dt><span class="term">
<code class="literal">\dAf[+]
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>input-type-pattern</code></em></a>]]
</code>
</span></dt><dd><p>
Lists operator families
(see <a class="xref" href="xindex.html#XINDEX-OPFAMILY" title="38.16.5. Operator Classes and Operator Families">Section 38.16.5</a>).
If <em class="replaceable"><code>access-method-pattern</code></em>
is specified, only operator families associated with access methods whose
names match that pattern are listed.
If <em class="replaceable"><code>input-type-pattern</code></em>
is specified, only operator families associated with input types whose
names match that pattern are listed.
If <code class="literal">+</code> is appended to the command name, each operator
family is listed with its owner.
</p></dd><dt><span class="term">
<code class="literal">\dAo[+]
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>operator-family-pattern</code></em></a>]]
</code>
</span></dt><dd><p>
Lists operators associated with operator families
(see <a class="xref" href="xindex.html#XINDEX-STRATEGIES" title="38.16.2. Index Method Strategies">Section 38.16.2</a>).
If <em class="replaceable"><code>access-method-pattern</code></em>
is specified, only members of operator families associated with access
methods whose names match that pattern are listed.
If <em class="replaceable"><code>operator-family-pattern</code></em>
is specified, only members of operator families whose names match that
pattern are listed.
If <code class="literal">+</code> is appended to the command name, each operator
is listed with its sort operator family (if it is an ordering operator).
</p></dd><dt><span class="term">
<code class="literal">\dAp[+]
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>access-method-pattern</code></em></a>
[<a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>operator-family-pattern</code></em></a>]]
</code>
</span></dt><dd><p>
Lists support functions associated with operator families
(see <a class="xref" href="xindex.html#XINDEX-SUPPORT" title="38.16.3. Index Method Support Routines">Section 38.16.3</a>).
If <em class="replaceable"><code>access-method-pattern</code></em>
is specified, only functions of operator families associated with
access methods whose names match that pattern are listed.
If <em class="replaceable"><code>operator-family-pattern</code></em>
is specified, only functions of operator families whose names match
that pattern are listed.
If <code class="literal">+</code> is appended to the command name, functions are
displayed verbosely, with their actual parameter lists.
</p></dd><dt><span class="term"><code class="literal">\db[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists tablespaces. If <em class="replaceable"><code>pattern</code></em>
is specified, only tablespaces whose names match the pattern are shown.
If <code class="literal">+</code> is appended to the command name, each tablespace
is listed with its associated options, on-disk size, permissions and
description.
</p></dd><dt><span class="term"><code class="literal">\dc[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists conversions between character-set encodings.
If <em class="replaceable"><code>pattern</code></em>
is specified, only conversions whose names match the pattern are
listed.
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
If <code class="literal">+</code> is appended to the command name, each object
is listed with its associated description.
</p></dd><dt><span class="term"><code class="literal">\dC[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists type casts.
If <em class="replaceable"><code>pattern</code></em>
is specified, only casts whose source or target types match the
pattern are listed.
If <code class="literal">+</code> is appended to the command name, each object
is listed with its associated description.
</p></dd><dt><span class="term"><code class="literal">\dd[S] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Shows the descriptions of objects of type <code class="literal">constraint</code>,
<code class="literal">operator class</code>, <code class="literal">operator family</code>,
<code class="literal">rule</code>, and <code class="literal">trigger</code>. All
other comments may be viewed by the respective backslash commands for
those object types.
</p><p><code class="literal">\dd</code> displays descriptions for objects matching the
<em class="replaceable"><code>pattern</code></em>, or of visible
objects of the appropriate type if no argument is given. But in either
case, only objects that have a description are listed.
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
</p><p>
Descriptions for objects can be created with the <a class="link" href="sql-comment.html" title="COMMENT"><code class="command">COMMENT</code></a>
<acronym class="acronym">SQL</acronym> command.
</p></dd><dt><span class="term"><code class="literal">\dD[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists domains. If <em class="replaceable"><code>pattern</code></em>
is specified, only domains whose names match the pattern are shown.
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
If <code class="literal">+</code> is appended to the command name, each object
is listed with its associated permissions and description.
</p></dd><dt><span class="term"><code class="literal">\ddp [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists default access privilege settings. An entry is shown for
each role (and schema, if applicable) for which the default
privilege settings have been changed from the built-in defaults.
If <em class="replaceable"><code>pattern</code></em> is
specified, only entries whose role name or schema name matches
the pattern are listed.
</p><p>
The <a class="link" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES"><code class="command">ALTER DEFAULT
PRIVILEGES</code></a> command is used to set default access
privileges. The meaning of the privilege display is explained in
<a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
</p></dd><dt><span class="term"><code class="literal">\dE[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\di[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dm[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\ds[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dt[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dv[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
In this group of commands, the letters <code class="literal">E</code>,
<code class="literal">i</code>, <code class="literal">m</code>, <code class="literal">s</code>,
<code class="literal">t</code>, and <code class="literal">v</code>
stand for foreign table, index, materialized view,
sequence, table, and view,
respectively.
You can specify any or all of
these letters, in any order, to obtain a listing of objects
of these types. For example, <code class="literal">\dti</code> lists
tables and indexes. If <code class="literal">+</code> is
appended to the command name, each object is listed with its
persistence status (permanent, temporary, or unlogged),
physical size on disk, and associated description if any.
If <em class="replaceable"><code>pattern</code></em> is
specified, only objects whose names match the pattern are listed.
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
</p></dd><dt><span class="term"><code class="literal">\des[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists foreign servers (mnemonic: <span class="quote">“<span class="quote">external
servers</span>”</span>).
If <em class="replaceable"><code>pattern</code></em> is
specified, only those servers whose name matches the pattern
are listed. If the form <code class="literal">\des+</code> is used, a
full description of each server is shown, including the
server's access privileges, type, version, options, and description.
</p></dd><dt><span class="term"><code class="literal">\det[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists foreign tables (mnemonic: <span class="quote">“<span class="quote">external tables</span>”</span>).
If <em class="replaceable"><code>pattern</code></em> is
specified, only entries whose table name or schema name matches
the pattern are listed. If the form <code class="literal">\det+</code>
is used, generic options and the foreign table description
are also displayed.
</p></dd><dt><span class="term"><code class="literal">\deu[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists user mappings (mnemonic: <span class="quote">“<span class="quote">external
users</span>”</span>).
If <em class="replaceable"><code>pattern</code></em> is
specified, only those mappings whose user names match the
pattern are listed. If the form <code class="literal">\deu+</code> is
used, additional information about each mapping is shown.
</p><div class="caution"><h3 class="title">Caution</h3><p>
<code class="literal">\deu+</code> might also display the user name and
password of the remote user, so care should be taken not to
disclose them.
</p></div></dd><dt><span class="term"><code class="literal">\dew[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists foreign-data wrappers (mnemonic: <span class="quote">“<span class="quote">external
wrappers</span>”</span>).
If <em class="replaceable"><code>pattern</code></em> is
specified, only those foreign-data wrappers whose name matches
the pattern are listed. If the form <code class="literal">\dew+</code>
is used, the access privileges, options, and description of the
foreign-data wrapper are also shown.
</p></dd><dt><span class="term"><code class="literal">\df[anptwS+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> [ <em class="replaceable"><code>arg_pattern</code></em> ... ] ]</code></span></dt><dd><p>
Lists functions, together with their result data types, argument data
types, and function types, which are classified as <span class="quote">“<span class="quote">agg</span>”</span>
(aggregate), <span class="quote">“<span class="quote">normal</span>”</span>, <span class="quote">“<span class="quote">procedure</span>”</span>, <span class="quote">“<span class="quote">trigger</span>”</span>, or <span class="quote">“<span class="quote">window</span>”</span>.
To display only functions
of specific type(s), add the corresponding letters <code class="literal">a</code>,
<code class="literal">n</code>, <code class="literal">p</code>, <code class="literal">t</code>, or <code class="literal">w</code> to the command.
If <em class="replaceable"><code>pattern</code></em> is specified, only
functions whose names match the pattern are shown.
Any additional arguments are type-name patterns, which are matched
to the type names of the first, second, and so on arguments of the
function. (Matching functions can have more arguments than what
you specify. To prevent that, write a dash <code class="literal">-</code> as
the last <em class="replaceable"><code>arg_pattern</code></em>.)
By default, only user-created
objects are shown; supply a pattern or the <code class="literal">S</code>
modifier to include system objects.
If the form <code class="literal">\df+</code> is used, additional information
about each function is shown, including volatility,
parallel safety, owner, security classification, access privileges,
language, source code and description.
</p></dd><dt><span class="term"><code class="literal">\dF[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists text search configurations.
If <em class="replaceable"><code>pattern</code></em> is specified,
only configurations whose names match the pattern are shown.
If the form <code class="literal">\dF+</code> is used, a full description of
each configuration is shown, including the underlying text search
parser and the dictionary list for each parser token type.
</p></dd><dt><span class="term"><code class="literal">\dFd[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists text search dictionaries.
If <em class="replaceable"><code>pattern</code></em> is specified,
only dictionaries whose names match the pattern are shown.
If the form <code class="literal">\dFd+</code> is used, additional information
is shown about each selected dictionary, including the underlying
text search template and the option values.
</p></dd><dt><span class="term"><code class="literal">\dFp[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists text search parsers.
If <em class="replaceable"><code>pattern</code></em> is specified,
only parsers whose names match the pattern are shown.
If the form <code class="literal">\dFp+</code> is used, a full description of
each parser is shown, including the underlying functions and the
list of recognized token types.
</p></dd><dt><span class="term"><code class="literal">\dFt[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists text search templates.
If <em class="replaceable"><code>pattern</code></em> is specified,
only templates whose names match the pattern are shown.
If the form <code class="literal">\dFt+</code> is used, additional information
is shown about each template, including the underlying function names.
</p></dd><dt><span class="term"><code class="literal">\dg[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists database roles.
(Since the concepts of <span class="quote">“<span class="quote">users</span>”</span> and <span class="quote">“<span class="quote">groups</span>”</span> have been
unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
<code class="literal">\du</code>.)
By default, only user-created roles are shown; supply the
<code class="literal">S</code> modifier to include system roles.
If <em class="replaceable"><code>pattern</code></em> is specified,
only those roles whose names match the pattern are listed.
If the form <code class="literal">\dg+</code> is used, additional information
is shown about each role; currently this adds the comment for each
role.
</p></dd><dt><span class="term"><code class="literal">\dl</code></span></dt><dd><p>
This is an alias for <code class="command">\lo_list</code>, which shows a
list of large objects.
</p></dd><dt><span class="term"><code class="literal">\dL[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists procedural languages. If <em class="replaceable"><code>pattern</code></em>
is specified, only languages whose names match the pattern are listed.
By default, only user-created languages
are shown; supply the <code class="literal">S</code> modifier to include system
objects. If <code class="literal">+</code> is appended to the command name, each
language is listed with its call handler, validator, access privileges,
and whether it is a system object.
</p></dd><dt><span class="term"><code class="literal">\dn[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists schemas (namespaces). If <em class="replaceable"><code>pattern</code></em>
is specified, only schemas whose names match the pattern are listed.
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system objects.
If <code class="literal">+</code> is appended to the command name, each object
is listed with its associated permissions and description, if any.
</p></dd><dt><span class="term"><code class="literal">\do[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> [ <em class="replaceable"><code>arg_pattern</code></em> [ <em class="replaceable"><code>arg_pattern</code></em> ] ] ]</code></span></dt><dd><p>
Lists operators with their operand and result types.
If <em class="replaceable"><code>pattern</code></em> is
specified, only operators whose names match the pattern are listed.
If one <em class="replaceable"><code>arg_pattern</code></em> is
specified, only prefix operators whose right argument's type name
matches that pattern are listed.
If two <em class="replaceable"><code>arg_pattern</code></em>s
are specified, only binary operators whose argument type names match
those patterns are listed. (Alternatively, write <code class="literal">-</code>
for the unused argument of a unary operator.)
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
If <code class="literal">+</code> is appended to the command name,
additional information about each operator is shown, currently just
the name of the underlying function.
</p></dd><dt><span class="term"><code class="literal">\dO[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists collations.
If <em class="replaceable"><code>pattern</code></em> is
specified, only collations whose names match the pattern are
listed. By default, only user-created objects are shown;
supply a pattern or the <code class="literal">S</code> modifier to
include system objects. If <code class="literal">+</code> is appended
to the command name, each collation is listed with its associated
description, if any.
Note that only collations usable with the current database's encoding
are shown, so the results may vary in different databases of the
same installation.
</p></dd><dt><span class="term"><code class="literal">\dp [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists tables, views and sequences with their
associated access privileges.
If <em class="replaceable"><code>pattern</code></em> is
specified, only tables, views and sequences whose names match the
pattern are listed.
</p><p>
The <a class="link" href="sql-grant.html" title="GRANT"><code class="command">GRANT</code></a> and
<a class="link" href="sql-revoke.html" title="REVOKE"><code class="command">REVOKE</code></a>
commands are used to set access privileges. The meaning of the
privilege display is explained in
<a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
</p></dd><dt><span class="term"><code class="literal">\dP[itn+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists partitioned relations.
If <em class="replaceable"><code>pattern</code></em>
is specified, only entries whose name matches the pattern are listed.
The modifiers <code class="literal">t</code> (tables) and <code class="literal">i</code>
(indexes) can be appended to the command, filtering the kind of
relations to list. By default, partitioned tables and indexes are
listed.
</p><p>
If the modifier <code class="literal">n</code> (<span class="quote">“<span class="quote">nested</span>”</span>) is used,
or a pattern is specified, then non-root partitioned relations are
included, and a column is shown displaying the parent of each
partitioned relation.
</p><p>
If <code class="literal">+</code> is appended to the command name, the sum of the
sizes of each relation's partitions is also displayed, along with the
relation's description.
If <code class="literal">n</code> is combined with <code class="literal">+</code>, two
sizes are shown: one including the total size of directly-attached
leaf partitions, and another showing the total size of all partitions,
including indirectly attached sub-partitions.
</p></dd><dt><span class="term"><code class="literal">\drds [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>role-pattern</code></em></a> [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>database-pattern</code></em></a> ] ]</code></span></dt><dd><p>
Lists defined configuration settings. These settings can be
role-specific, database-specific, or both.
<em class="replaceable"><code>role-pattern</code></em> and
<em class="replaceable"><code>database-pattern</code></em> are used to select
specific roles and databases to list, respectively. If omitted, or if
<code class="literal">*</code> is specified, all settings are listed, including those
not role-specific or database-specific, respectively.
</p><p>
The <a class="link" href="sql-alterrole.html" title="ALTER ROLE"><code class="command">ALTER ROLE</code></a> and
<a class="link" href="sql-alterdatabase.html" title="ALTER DATABASE"><code class="command">ALTER DATABASE</code></a>
commands are used to define per-role and per-database configuration
settings.
</p></dd><dt><span class="term"><code class="literal">\dRp[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists replication publications.
If <em class="replaceable"><code>pattern</code></em> is
specified, only those publications whose names match the pattern are
listed.
If <code class="literal">+</code> is appended to the command name, the tables
associated with each publication are shown as well.
</p></dd><dt><span class="term"><code class="literal">\dRs[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists replication subscriptions.
If <em class="replaceable"><code>pattern</code></em> is
specified, only those subscriptions whose names match the pattern are
listed.
If <code class="literal">+</code> is appended to the command name, additional
properties of the subscriptions are shown.
</p></dd><dt><span class="term"><code class="literal">\dT[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists data types.
If <em class="replaceable"><code>pattern</code></em> is
specified, only types whose names match the pattern are listed.
If <code class="literal">+</code> is appended to the command name, each type is
listed with its internal name and size, its allowed values
if it is an <code class="type">enum</code> type, and its associated permissions.
By default, only user-created objects are shown; supply a
pattern or the <code class="literal">S</code> modifier to include system
objects.
</p></dd><dt><span class="term"><code class="literal">\du[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists database roles.
(Since the concepts of <span class="quote">“<span class="quote">users</span>”</span> and <span class="quote">“<span class="quote">groups</span>”</span> have been
unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
<code class="literal">\dg</code>.)
By default, only user-created roles are shown; supply the
<code class="literal">S</code> modifier to include system roles.
If <em class="replaceable"><code>pattern</code></em> is specified,
only those roles whose names match the pattern are listed.
If the form <code class="literal">\du+</code> is used, additional information
is shown about each role; currently this adds the comment for each
role.
</p></dd><dt><span class="term"><code class="literal">\dx[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists installed extensions.
If <em class="replaceable"><code>pattern</code></em>
is specified, only those extensions whose names match the pattern
are listed.
If the form <code class="literal">\dx+</code> is used, all the objects belonging
to each matching extension are listed.
</p></dd><dt><span class="term"><code class="literal">\dX [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists extended statistics.
If <em class="replaceable"><code>pattern</code></em>
is specified, only those extended statistics whose names match the
pattern are listed.
</p><p>
The status of each kind of extended statistics is shown in a column
named after its statistic kind (e.g. Ndistinct).
<code class="literal">defined</code> means that it was requested when creating
the statistics, and NULL means it wasn't requested.
You can use <code class="structname">pg_stats_ext</code> if you'd like to
know whether <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>
was run and statistics are available to the planner.
</p></dd><dt><span class="term"><code class="literal">\dy[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists event triggers.
If <em class="replaceable"><code>pattern</code></em>
is specified, only those event triggers whose names match the pattern
are listed.
If <code class="literal">+</code> is appended to the command name, each object
is listed with its associated description.
</p></dd><dt><span class="term"><code class="literal">\e</code> or <code class="literal">\edit</code> <code class="literal"> [<span class="optional"> <em class="replaceable"><code>filename</code></em> </span>] [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </code></span></dt><dd><p>
If <em class="replaceable"><code>filename</code></em> is
specified, the file is edited; after the editor exits, the file's
content is copied into the current query buffer. If no <em class="replaceable"><code>filename</code></em> is given, the current query
buffer is copied to a temporary file which is then edited in the same
fashion. Or, if the current query buffer is empty, the most recently
executed query is copied to a temporary file and edited in the same
fashion.
</p><p>
If you edit a file or the previous query, and you quit the editor without
modifying the file, the query buffer is cleared.
Otherwise, the new contents of the query buffer are re-parsed according to
the normal rules of <span class="application">psql</span>, treating the
whole buffer as a single line. Any complete queries are immediately
executed; that is, if the query buffer contains or ends with a
semicolon, everything up to that point is executed and removed from
the query buffer. Whatever remains in the query buffer is
redisplayed. Type semicolon or <code class="literal">\g</code> to send it,
or <code class="literal">\r</code> to cancel it by clearing the query buffer.
</p><p>
Treating the buffer as a single line primarily affects meta-commands:
whatever is in the buffer after a meta-command will be taken as
argument(s) to the meta-command, even if it spans multiple lines.
(Thus you cannot make meta-command-using scripts this way.
Use <code class="command">\i</code> for that.)
</p><p>
If a line number is specified, <span class="application">psql</span> will
position the cursor on the specified line of the file or query buffer.
Note that if a single all-digits argument is given,
<span class="application">psql</span> assumes it is a line number,
not a file name.
</p><div class="tip"><h3 class="title">Tip</h3><p>
See <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a>, below, for how to
configure and customize your editor.
</p></div></dd><dt><span class="term"><code class="literal">\echo <em class="replaceable"><code>text</code></em> [ ... ]</code></span></dt><dd><p>
Prints the evaluated arguments to standard output, separated by
spaces and followed by a newline. This can be useful to
intersperse information in the output of scripts. For example:
</p><pre class="programlisting">
=> <strong class="userinput"><code>\echo `date`</code></strong>
Tue Oct 26 21:40:57 CEST 1999
</pre><p>
If the first argument is an unquoted <code class="literal">-n</code> the trailing
newline is not written (nor is the first argument).
</p><div class="tip"><h3 class="title">Tip</h3><p>
If you use the <code class="command">\o</code> command to redirect your
query output you might wish to use <code class="command">\qecho</code>
instead of this command. See also <code class="command">\warn</code>.
</p></div></dd><dt><span class="term"><code class="literal">\ef [<span class="optional"> <em class="replaceable"><code>function_description</code></em> [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </span>] </code></span></dt><dd><p>
This command fetches and edits the definition of the named function or procedure,
in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
<code class="command">CREATE OR REPLACE PROCEDURE</code> command.
Editing is done in the same way as for <code class="literal">\edit</code>.
If you quit the editor without saving, the statement is discarded.
If you save and exit the editor, the updated command is executed immediately
if you added a semicolon to it. Otherwise it is redisplayed;
type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
to cancel.
</p><p>
The target function can be specified by name alone, or by name
and arguments, for example <code class="literal">foo(integer, text)</code>.
The argument types must be given if there is more
than one function of the same name.
</p><p>
If no function is specified, a blank <code class="command">CREATE FUNCTION</code>
template is presented for editing.
</p><p>
If a line number is specified, <span class="application">psql</span> will
position the cursor on the specified line of the function body.
(Note that the function body typically does not begin on the first
line of the file.)
</p><p>
Unlike most other meta-commands, the entire remainder of the line is
always taken to be the argument(s) of <code class="command">\ef</code>, and neither
variable interpolation nor backquote expansion are performed in the
arguments.
</p><div class="tip"><h3 class="title">Tip</h3><p>
See <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a>, below, for how to
configure and customize your editor.
</p></div></dd><dt><span class="term"><code class="literal">\encoding [ <em class="replaceable"><code>encoding</code></em> ]</code></span></dt><dd><p>
Sets the client character set encoding. Without an argument, this command
shows the current encoding.
</p></dd><dt><span class="term"><code class="literal">\errverbose</code></span></dt><dd><p>
Repeats the most recent server error message at maximum
verbosity, as though <code class="varname">VERBOSITY</code> were set
to <code class="literal">verbose</code> and <code class="varname">SHOW_CONTEXT</code> were
set to <code class="literal">always</code>.
</p></dd><dt><span class="term"><code class="literal">\ev [<span class="optional"> <em class="replaceable"><code>view_name</code></em> [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </span>] </code></span></dt><dd><p>
This command fetches and edits the definition of the named view,
in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
Editing is done in the same way as for <code class="literal">\edit</code>.
If you quit the editor without saving, the statement is discarded.
If you save and exit the editor, the updated command is executed immediately
if you added a semicolon to it. Otherwise it is redisplayed;
type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
to cancel.
</p><p>
If no view is specified, a blank <code class="command">CREATE VIEW</code>
template is presented for editing.
</p><p>
If a line number is specified, <span class="application">psql</span> will
position the cursor on the specified line of the view definition.
</p><p>
Unlike most other meta-commands, the entire remainder of the line is
always taken to be the argument(s) of <code class="command">\ev</code>, and neither
variable interpolation nor backquote expansion are performed in the
arguments.
</p></dd><dt><span class="term"><code class="literal">\f [ <em class="replaceable"><code>string</code></em> ]</code></span></dt><dd><p>
Sets the field separator for unaligned query output. The default
is the vertical bar (<code class="literal">|</code>). It is equivalent to
<code class="command">\pset fieldsep</code>.
</p></dd><dt><span class="term"><code class="literal">\g [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\g [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
Sends the current query buffer to the server for execution.
</p><p>
If parentheses appear after <code class="literal">\g</code>, they surround a
space-separated list
of <em class="replaceable"><code>option</code></em><code class="literal">=</code><em class="replaceable"><code>value</code></em>
formatting-option clauses, which are interpreted in the same way
as <code class="literal">\pset</code>
<em class="replaceable"><code>option</code></em>
<em class="replaceable"><code>value</code></em> commands, but take
effect only for the duration of this query. In this list, spaces are
not allowed around <code class="literal">=</code> signs, but are required
between option clauses.
If <code class="literal">=</code><em class="replaceable"><code>value</code></em>
is omitted, the
named <em class="replaceable"><code>option</code></em> is changed
in the same way as for
<code class="literal">\pset</code> <em class="replaceable"><code>option</code></em>
with no explicit <em class="replaceable"><code>value</code></em>.
</p><p>
If a <em class="replaceable"><code>filename</code></em>
or <code class="literal">|</code><em class="replaceable"><code>command</code></em>
argument is given, the query's output is written to the named
file or piped to the given shell command, instead of displaying it as
usual. The file or command is written to only if the query
successfully returns zero or more tuples, not if the query fails or
is a non-data-returning SQL command.
</p><p>
If the current query buffer is empty, the most recently sent query is
re-executed instead. Except for that behavior, <code class="literal">\g</code>
without any arguments is essentially equivalent to a semicolon.
With arguments, <code class="literal">\g</code> provides
a <span class="quote">“<span class="quote">one-shot</span>”</span> alternative to the <code class="command">\o</code>
command, and additionally allows one-shot adjustments of the
output formatting options normally set by <code class="literal">\pset</code>.
</p><p>
When the last argument begins with <code class="literal">|</code>, the entire
remainder of the line is taken to be
the <em class="replaceable"><code>command</code></em> to execute,
and neither variable interpolation nor backquote expansion are
performed in it. The rest of the line is simply passed literally to
the shell.
</p></dd><dt><span class="term"><code class="literal">\gdesc</code></span></dt><dd><p>
Shows the description (that is, the column names and data types)
of the result of the current query buffer. The query is not
actually executed; however, if it contains some type of syntax
error, that error will be reported in the normal way.
</p><p>
If the current query buffer is empty, the most recently sent query
is described instead.
</p></dd><dt><span class="term"><code class="literal">\gexec</code></span></dt><dd><p>
Sends the current query buffer to the server, then treats
each column of each row of the query's output (if any) as an SQL
statement to be executed. For example, to create an index on each
column of <code class="structname">my_table</code>:
</p><pre class="programlisting">
=> <strong class="userinput"><code>SELECT format('create index on my_table(%I)', attname)</code></strong>
-> <strong class="userinput"><code>FROM pg_attribute</code></strong>
-> <strong class="userinput"><code>WHERE attrelid = 'my_table'::regclass AND attnum > 0</code></strong>
-> <strong class="userinput"><code>ORDER BY attnum</code></strong>
-> <strong class="userinput"><code>\gexec</code></strong>
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
</pre><p>
</p><p>
The generated queries are executed in the order in which the rows
are returned, and left-to-right within each row if there is more
than one column. NULL fields are ignored. The generated queries
are sent literally to the server for processing, so they cannot be
<span class="application">psql</span> meta-commands nor contain <span class="application">psql</span>
variable references. If any individual query fails, execution of
the remaining queries continues
unless <code class="varname">ON_ERROR_STOP</code> is set. Execution of each
query is subject to <code class="varname">ECHO</code> processing.
(Setting <code class="varname">ECHO</code> to <code class="literal">all</code>
or <code class="literal">queries</code> is often advisable when
using <code class="command">\gexec</code>.) Query logging, single-step mode,
timing, and other query execution features apply to each generated
query as well.
</p><p>
If the current query buffer is empty, the most recently sent query
is re-executed instead.
</p></dd><dt><span class="term"><code class="literal">\gset [ <em class="replaceable"><code>prefix</code></em> ]</code></span></dt><dd><p>
Sends the current query buffer to the server and stores the
query's output into <span class="application">psql</span> variables
(see <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below).
The query to be executed must return exactly one row. Each column of
the row is stored into a separate variable, named the same as the
column. For example:
</p><pre class="programlisting">
=> <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
-> <strong class="userinput"><code>\gset</code></strong>
=> <strong class="userinput"><code>\echo :var1 :var2</code></strong>
hello 10
</pre><p>
</p><p>
If you specify a <em class="replaceable"><code>prefix</code></em>,
that string is prepended to the query's column names to create the
variable names to use:
</p><pre class="programlisting">
=> <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
-> <strong class="userinput"><code>\gset result_</code></strong>
=> <strong class="userinput"><code>\echo :result_var1 :result_var2</code></strong>
hello 10
</pre><p>
</p><p>
If a column result is NULL, the corresponding variable is unset
rather than being set.
</p><p>
If the query fails or does not return one row,
no variables are changed.
</p><p>
If the current query buffer is empty, the most recently sent query
is re-executed instead.
</p></dd><dt><span class="term"><code class="literal">\gx [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\gx [ (<em class="replaceable"><code>option</code></em>=<em class="replaceable"><code>value</code></em> [...]) ] [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
<code class="literal">\gx</code> is equivalent to <code class="literal">\g</code>, except
that it forces expanded output mode for this query, as
if <code class="literal">expanded=on</code> were included in the list of
<code class="literal">\pset</code> options. See also <code class="literal">\x</code>.
</p></dd><dt><span class="term"><code class="literal">\h</code> or <code class="literal">\help</code> <code class="literal">[ <em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
Gives syntax help on the specified <acronym class="acronym">SQL</acronym>
command. If <em class="replaceable"><code>command</code></em>
is not specified, then <span class="application">psql</span> will list
all the commands for which syntax help is available. If
<em class="replaceable"><code>command</code></em> is an
asterisk (<code class="literal">*</code>), then syntax help on all
<acronym class="acronym">SQL</acronym> commands is shown.
</p><p>
Unlike most other meta-commands, the entire remainder of the line is
always taken to be the argument(s) of <code class="command">\help</code>, and neither
variable interpolation nor backquote expansion are performed in the
arguments.
</p><div class="note"><h3 class="title">Note</h3><p>
To simplify typing, commands that consists of several words do
not have to be quoted. Thus it is fine to type <strong class="userinput"><code>\help
alter table</code></strong>.
</p></div></dd><dt><span class="term"><code class="literal">\H</code> or <code class="literal">\html</code></span></dt><dd><p>
Turns on <acronym class="acronym">HTML</acronym> query output format. If the
<acronym class="acronym">HTML</acronym> format is already on, it is switched
back to the default aligned text format. This command is for
compatibility and convenience, but see <code class="command">\pset</code>
about setting other output options.
</p></dd><dt><span class="term"><code class="literal">\i</code> or <code class="literal">\include</code> <em class="replaceable"><code>filename</code></em></span></dt><dd><p>
Reads input from the file <em class="replaceable"><code>filename</code></em> and executes it as
though it had been typed on the keyboard.
</p><p>
If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
(hyphen), then standard input is read until an EOF indication
or <code class="command">\q</code> meta-command. This can be used to intersperse
interactive input with input from files. Note that Readline behavior
will be used only if it is active at the outermost level.
</p><div class="note"><h3 class="title">Note</h3><p>
If you want to see the lines on the screen as they are read you
must set the variable <code class="varname">ECHO</code> to
<code class="literal">all</code>.
</p></div></dd><dt id="PSQL-METACOMMAND-IF"><span class="term"><code class="literal">\if</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\elif</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\else</code><br /></span><span class="term"><code class="literal">\endif</code></span></dt><dd><p>
This group of commands implements nestable conditional blocks.
A conditional block must begin with an <code class="command">\if</code> and end
with an <code class="command">\endif</code>. In between there may be any number
of <code class="command">\elif</code> clauses, which may optionally be followed
by a single <code class="command">\else</code> clause. Ordinary queries and
other types of backslash commands may (and usually do) appear between
the commands forming a conditional block.
</p><p>
The <code class="command">\if</code> and <code class="command">\elif</code> commands read
their argument(s) and evaluate them as a Boolean expression. If the
expression yields <code class="literal">true</code> then processing continues
normally; otherwise, lines are skipped until a
matching <code class="command">\elif</code>, <code class="command">\else</code>,
or <code class="command">\endif</code> is reached. Once
an <code class="command">\if</code> or <code class="command">\elif</code> test has
succeeded, the arguments of later <code class="command">\elif</code> commands in
the same block are not evaluated but are treated as false. Lines
following an <code class="command">\else</code> are processed only if no earlier
matching <code class="command">\if</code> or <code class="command">\elif</code> succeeded.
</p><p>
The <em class="replaceable"><code>expression</code></em> argument
of an <code class="command">\if</code> or <code class="command">\elif</code> command
is subject to variable interpolation and backquote expansion, just
like any other backslash command argument. After that it is evaluated
like the value of an on/off option variable. So a valid value
is any unambiguous case-insensitive match for one of:
<code class="literal">true</code>, <code class="literal">false</code>, <code class="literal">1</code>,
<code class="literal">0</code>, <code class="literal">on</code>, <code class="literal">off</code>,
<code class="literal">yes</code>, <code class="literal">no</code>. For example,
<code class="literal">t</code>, <code class="literal">T</code>, and <code class="literal">tR</code>
will all be considered to be <code class="literal">true</code>.
</p><p>
Expressions that do not properly evaluate to true or false will
generate a warning and be treated as false.
</p><p>
Lines being skipped are parsed normally to identify queries and
backslash commands, but queries are not sent to the server, and
backslash commands other than conditionals
(<code class="command">\if</code>, <code class="command">\elif</code>,
<code class="command">\else</code>, <code class="command">\endif</code>) are
ignored. Conditional commands are checked only for valid nesting.
Variable references in skipped lines are not expanded, and backquote
expansion is not performed either.
</p><p>
All the backslash commands of a given conditional block must appear in
the same source file. If EOF is reached on the main input file or an
<code class="command">\include</code>-ed file before all local
<code class="command">\if</code>-blocks have been closed,
then <span class="application">psql</span> will raise an error.
</p><p>
Here is an example:
</p><pre class="programlisting">
-- check for the existence of two separate records in the database and store
-- the results in separate psql variables
SELECT
EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
\gset
\if :is_customer
SELECT * FROM customer WHERE customer_id = 123;
\elif :is_employee
\echo 'is not a customer but is an employee'
SELECT * FROM employee WHERE employee_id = 456;
\else
\if yes
\echo 'not a customer or employee'
\else
\echo 'this will never print'
\endif
\endif
</pre></dd><dt><span class="term"><code class="literal">\ir</code> or <code class="literal">\include_relative</code> <em class="replaceable"><code>filename</code></em></span></dt><dd><p>
The <code class="literal">\ir</code> command is similar to <code class="literal">\i</code>, but resolves
relative file names differently. When executing in interactive mode,
the two commands behave identically. However, when invoked from a
script, <code class="literal">\ir</code> interprets file names relative to the
directory in which the script is located, rather than the current
working directory.
</p></dd><dt><span class="term"><code class="literal">\l[+]</code> or <code class="literal">\list[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
List the databases in the server and show their names, owners,
character set encodings, and access privileges.
If <em class="replaceable"><code>pattern</code></em> is specified,
only databases whose names match the pattern are listed.
If <code class="literal">+</code> is appended to the command name, database
sizes, default tablespaces, and descriptions are also displayed.
(Size information is only available for databases that the current
user can connect to.)
</p></dd><dt><span class="term"><code class="literal">\lo_export <em class="replaceable"><code>loid</code></em> <em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
Reads the large object with <acronym class="acronym">OID</acronym> <em class="replaceable"><code>loid</code></em> from the database and
writes it to <em class="replaceable"><code>filename</code></em>. Note that this is
subtly different from the server function
<code class="function">lo_export</code>, which acts with the permissions
of the user that the database server runs as and on the server's
file system.
</p><div class="tip"><h3 class="title">Tip</h3><p>
Use <code class="command">\lo_list</code> to find out the large object's
<acronym class="acronym">OID</acronym>.
</p></div></dd><dt><span class="term"><code class="literal">\lo_import <em class="replaceable"><code>filename</code></em> [ <em class="replaceable"><code>comment</code></em> ]</code></span></dt><dd><p>
Stores the file into a <span class="productname">PostgreSQL</span>
large object. Optionally, it associates the given
comment with the object. Example:
</p><pre class="programlisting">
foo=> <strong class="userinput"><code>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</code></strong>
lo_import 152801
</pre><p>
The response indicates that the large object received object
ID 152801, which can be used to access the newly-created large
object in the future. For the sake of readability, it is
recommended to always associate a human-readable comment with
every object. Both OIDs and comments can be viewed with the
<code class="command">\lo_list</code> command.
</p><p>
Note that this command is subtly different from the server-side
<code class="function">lo_import</code> because it acts as the local user
on the local file system, rather than the server's user and file
system.
</p></dd><dt><span class="term"><code class="literal">\lo_list</code></span></dt><dd><p>
Shows a list of all <span class="productname">PostgreSQL</span>
large objects currently stored in the database,
along with any comments provided for them.
</p></dd><dt><span class="term"><code class="literal">\lo_unlink <em class="replaceable"><code>loid</code></em></code></span></dt><dd><p>
Deletes the large object with <acronym class="acronym">OID</acronym>
<em class="replaceable"><code>loid</code></em> from the
database.
</p><div class="tip"><h3 class="title">Tip</h3><p>
Use <code class="command">\lo_list</code> to find out the large object's
<acronym class="acronym">OID</acronym>.
</p></div></dd><dt><span class="term"><code class="literal">\o</code> or <code class="literal">\out [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\o</code> or <code class="literal">\out [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
Arranges to save future query results to the file <em class="replaceable"><code>filename</code></em> or pipe future results
to the shell command <em class="replaceable"><code>command</code></em>. If no argument is
specified, the query output is reset to the standard output.
</p><p>
If the argument begins with <code class="literal">|</code>, then the entire remainder
of the line is taken to be
the <em class="replaceable"><code>command</code></em> to execute,
and neither variable interpolation nor backquote expansion are
performed in it. The rest of the line is simply passed literally to
the shell.
</p><p>
<span class="quote">“<span class="quote">Query results</span>”</span> includes all tables, command
responses, and notices obtained from the database server, as
well as output of various backslash commands that query the
database (such as <code class="command">\d</code>); but not error
messages.
</p><div class="tip"><h3 class="title">Tip</h3><p>
To intersperse text output in between query results, use
<code class="command">\qecho</code>.
</p></div></dd><dt><span class="term"><code class="literal">\p</code> or <code class="literal">\print</code></span></dt><dd><p>
Print the current query buffer to the standard output.
If the current query buffer is empty, the most recently executed query
is printed instead.
</p></dd><dt><span class="term"><code class="literal">\password [ <em class="replaceable"><code>username</code></em> ]</code></span></dt><dd><p>
Changes the password of the specified user (by default, the current
user). This command prompts for the new password, encrypts it, and
sends it to the server as an <code class="command">ALTER ROLE</code> command. This
makes sure that the new password does not appear in cleartext in the
command history, the server log, or elsewhere.
</p></dd><dt><span class="term"><code class="literal">\prompt [ <em class="replaceable"><code>text</code></em> ] <em class="replaceable"><code>name</code></em></code></span></dt><dd><p>
Prompts the user to supply text, which is assigned to the variable
<em class="replaceable"><code>name</code></em>.
An optional prompt string, <em class="replaceable"><code>text</code></em>, can be specified. (For multiword
prompts, surround the text with single quotes.)
</p><p>
By default, <code class="literal">\prompt</code> uses the terminal for input and
output. However, if the <code class="option">-f</code> command line switch was
used, <code class="literal">\prompt</code> uses standard input and standard output.
</p></dd><dt><span class="term"><code class="literal">\pset [ <em class="replaceable"><code>option</code></em> [ <em class="replaceable"><code>value</code></em> ] ]</code></span></dt><dd><p>
This command sets options affecting the output of query result tables.
<em class="replaceable"><code>option</code></em>
indicates which option is to be set. The semantics of
<em class="replaceable"><code>value</code></em> vary depending
on the selected option. For some options, omitting <em class="replaceable"><code>value</code></em> causes the option to be toggled
or unset, as described under the particular option. If no such
behavior is mentioned, then omitting
<em class="replaceable"><code>value</code></em> just results in
the current setting being displayed.
</p><p>
<code class="command">\pset</code> without any arguments displays the current status
of all printing options.
</p><p>
Adjustable printing options are:
</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">border</code></span></dt><dd><p>
The <em class="replaceable"><code>value</code></em> must be a
number. In general, the higher
the number the more borders and lines the tables will have,
but details depend on the particular format.
In <acronym class="acronym">HTML</acronym> format, this will translate directly
into the <code class="literal">border=...</code> attribute.
In most other formats only values 0 (no border), 1 (internal
dividing lines), and 2 (table frame) make sense, and values above 2
will be treated the same as <code class="literal">border = 2</code>.
The <code class="literal">latex</code> and <code class="literal">latex-longtable</code>
formats additionally allow a value of 3 to add dividing lines
between data rows.
</p></dd><dt><span class="term"><code class="literal">columns</code></span></dt><dd><p>
Sets the target width for the <code class="literal">wrapped</code> format, and also
the width limit for determining whether output is wide enough to
require the pager or switch to the vertical display in expanded auto
mode.
Zero (the default) causes the target width to be controlled by the
environment variable <code class="envar">COLUMNS</code>, or the detected screen width
if <code class="envar">COLUMNS</code> is not set.
In addition, if <code class="literal">columns</code> is zero then the
<code class="literal">wrapped</code> format only affects screen output.
If <code class="literal">columns</code> is nonzero then file and pipe output is
wrapped to that width as well.
</p></dd><dt><span class="term"><code class="literal">csv_fieldsep</code></span></dt><dd><p>
Specifies the field separator to be used in
<acronym class="acronym">CSV</acronym> output format. If the separator character
appears in a field's value, that field is output within double
quotes, following standard <acronym class="acronym">CSV</acronym> rules.
The default is a comma.
</p></dd><dt><span class="term"><code class="literal">expanded</code> (or <code class="literal">x</code>)</span></dt><dd><p>
If <em class="replaceable"><code>value</code></em> is specified it
must be either <code class="literal">on</code> or <code class="literal">off</code>, which
will enable or disable expanded mode, or <code class="literal">auto</code>.
If <em class="replaceable"><code>value</code></em> is omitted the
command toggles between the on and off settings. When expanded mode
is enabled, query results are displayed in two columns, with the
column name on the left and the data on the right. This mode is
useful if the data wouldn't fit on the screen in the
normal <span class="quote">“<span class="quote">horizontal</span>”</span> mode. In the auto setting, the
expanded mode is used whenever the query output has more than one
column and is wider than the screen; otherwise, the regular mode is
used. The auto setting is only
effective in the aligned and wrapped formats. In other formats, it
always behaves as if the expanded mode is off.
</p></dd><dt><span class="term"><code class="literal">fieldsep</code></span></dt><dd><p>
Specifies the field separator to be used in unaligned output
format. That way one can create, for example, tab-separated
output, which other programs might prefer. To
set a tab as field separator, type <code class="literal">\pset fieldsep
'\t'</code>. The default field separator is
<code class="literal">'|'</code> (a vertical bar).
</p></dd><dt><span class="term"><code class="literal">fieldsep_zero</code></span></dt><dd><p>
Sets the field separator to use in unaligned output format to a zero
byte.
</p></dd><dt><span class="term"><code class="literal">footer</code></span></dt><dd><p>
If <em class="replaceable"><code>value</code></em> is specified
it must be either <code class="literal">on</code> or <code class="literal">off</code>
which will enable or disable display of the table footer
(the <code class="literal">(<em class="replaceable"><code>n</code></em> rows)</code> count).
If <em class="replaceable"><code>value</code></em> is omitted the
command toggles footer display on or off.
</p></dd><dt><span class="term"><code class="literal">format</code></span></dt><dd><p>
Sets the output format to one of <code class="literal">aligned</code>,
<code class="literal">asciidoc</code>,
<code class="literal">csv</code>,
<code class="literal">html</code>,
<code class="literal">latex</code>,
<code class="literal">latex-longtable</code>, <code class="literal">troff-ms</code>,
<code class="literal">unaligned</code>, or <code class="literal">wrapped</code>.
Unique abbreviations are allowed.
</p><p><code class="literal">aligned</code> format is the standard,
human-readable, nicely formatted text output; this is the default.
</p><p><code class="literal">unaligned</code> format writes all columns of a row on one
line, separated by the currently active field separator. This
is useful for creating output that might be intended to be read
in by other programs, for example, tab-separated or comma-separated
format. However, the field separator character is not treated
specially if it appears in a column's value;
so <acronym class="acronym">CSV</acronym> format may be better suited for such
purposes.
</p><p><code class="literal">csv</code> format
<a id="id-1.9.4.20.8.4.10.1.74.2.3.1.8.2.4.2" class="indexterm"></a>
writes column values separated by commas, applying the quoting
rules described in
<a class="ulink" href="https://tools.ietf.org/html/rfc4180" target="_top">RFC 4180</a>.
This output is compatible with the CSV format of the server's
<code class="command">COPY</code> command.
A header line with column names is generated unless
the <code class="literal">tuples_only</code> parameter is
<code class="literal">on</code>. Titles and footers are not printed.
Each row is terminated by the system-dependent end-of-line character,
which is typically a single newline (<code class="literal">\n</code>) for
Unix-like systems or a carriage return and newline sequence
(<code class="literal">\r\n</code>) for Microsoft Windows.
Field separator characters other than comma can be selected with
<code class="command">\pset csv_fieldsep</code>.
</p><p><code class="literal">wrapped</code> format is like <code class="literal">aligned</code> but wraps
wide data values across lines to make the output fit in the target
column width. The target width is determined as described under
the <code class="literal">columns</code> option. Note that <span class="application">psql</span> will
not attempt to wrap column header titles; therefore,
<code class="literal">wrapped</code> format behaves the same as <code class="literal">aligned</code>
if the total width needed for column headers exceeds the target.
</p><p>
The <code class="literal">asciidoc</code>, <code class="literal">html</code>,
<code class="literal">latex</code>, <code class="literal">latex-longtable</code>, and
<code class="literal">troff-ms</code> formats put out tables that are intended
to be included in documents using the respective mark-up
language. They are not complete documents! This might not be
necessary in <acronym class="acronym">HTML</acronym>, but in
<span class="application">LaTeX</span> you must have a complete
document wrapper.
The <code class="literal">latex</code> format
uses <span class="application">LaTeX</span>'s <code class="literal">tabular</code>
environment.
The <code class="literal">latex-longtable</code> format
requires the <span class="application">LaTeX</span>
<code class="literal">longtable</code> and <code class="literal">booktabs</code> packages.
</p></dd><dt><span class="term"><code class="literal">linestyle</code></span></dt><dd><p>
Sets the border line drawing style to one
of <code class="literal">ascii</code>, <code class="literal">old-ascii</code>,
or <code class="literal">unicode</code>.
Unique abbreviations are allowed. (That would mean one
letter is enough.)
The default setting is <code class="literal">ascii</code>.
This option only affects the <code class="literal">aligned</code> and
<code class="literal">wrapped</code> output formats.
</p><p><code class="literal">ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
characters. Newlines in data are shown using
a <code class="literal">+</code> symbol in the right-hand margin.
When the <code class="literal">wrapped</code> format wraps data from
one line to the next without a newline character, a dot
(<code class="literal">.</code>) is shown in the right-hand margin of the first line,
and again in the left-hand margin of the following line.
</p><p><code class="literal">old-ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
characters, using the formatting style used
in <span class="productname">PostgreSQL</span> 8.4 and earlier.
Newlines in data are shown using a <code class="literal">:</code>
symbol in place of the left-hand column separator.
When the data is wrapped from one line
to the next without a newline character, a <code class="literal">;</code>
symbol is used in place of the left-hand column separator.
</p><p><code class="literal">unicode</code> style uses Unicode box-drawing characters.
Newlines in data are shown using a carriage return symbol
in the right-hand margin. When the data is wrapped from one line
to the next without a newline character, an ellipsis symbol
is shown in the right-hand margin of the first line, and
again in the left-hand margin of the following line.
</p><p>
When the <code class="literal">border</code> setting is greater than zero,
the <code class="literal">linestyle</code> option also determines the
characters with which the border lines are drawn.
Plain <acronym class="acronym">ASCII</acronym> characters work everywhere, but
Unicode characters look nicer on displays that recognize them.
</p></dd><dt><span class="term"><code class="literal">null</code></span></dt><dd><p>
Sets the string to be printed in place of a null value.
The default is to print nothing, which can easily be mistaken for
an empty string. For example, one might prefer <code class="literal">\pset null
'(null)'</code>.
</p></dd><dt><span class="term"><code class="literal">numericlocale</code></span></dt><dd><p>
If <em class="replaceable"><code>value</code></em> is specified
it must be either <code class="literal">on</code> or <code class="literal">off</code>
which will enable or disable display of a locale-specific character
to separate groups of digits to the left of the decimal marker.
If <em class="replaceable"><code>value</code></em> is omitted the
command toggles between regular and locale-specific numeric output.
</p></dd><dt><span class="term"><code class="literal">pager</code></span></dt><dd><p>
Controls use of a pager program for query and <span class="application">psql</span>
help output. If the environment variable <code class="envar">PSQL_PAGER</code>
or <code class="envar">PAGER</code> is set, the output is piped to the
specified program. Otherwise a platform-dependent default program
(such as <code class="filename">more</code>) is used.
</p><p>
When the <code class="literal">pager</code> option is <code class="literal">off</code>, the pager
program is not used. When the <code class="literal">pager</code> option is
<code class="literal">on</code>, the pager is used when appropriate, i.e., when the
output is to a terminal and will not fit on the screen.
The <code class="literal">pager</code> option can also be set to <code class="literal">always</code>,
which causes the pager to be used for all terminal output regardless
of whether it fits on the screen. <code class="literal">\pset pager</code>
without a <em class="replaceable"><code>value</code></em>
toggles pager use on and off.
</p></dd><dt><span class="term"><code class="literal">pager_min_lines</code></span></dt><dd><p>
If <code class="literal">pager_min_lines</code> is set to a number greater than the
page height, the pager program will not be called unless there are
at least this many lines of output to show. The default setting
is 0.
</p></dd><dt><span class="term"><code class="literal">recordsep</code></span></dt><dd><p>
Specifies the record (line) separator to use in unaligned
output format. The default is a newline character.
</p></dd><dt><span class="term"><code class="literal">recordsep_zero</code></span></dt><dd><p>
Sets the record separator to use in unaligned output format to a zero
byte.
</p></dd><dt><span class="term"><code class="literal">tableattr</code> (or <code class="literal">T</code>)</span></dt><dd><p>
In <acronym class="acronym">HTML</acronym> format, this specifies attributes
to be placed inside the <code class="sgmltag-element">table</code> tag. This
could for example be <code class="literal">cellpadding</code> or
<code class="literal">bgcolor</code>. Note that you probably don't want
to specify <code class="literal">border</code> here, as that is already
taken care of by <code class="literal">\pset border</code>.
If no
<em class="replaceable"><code>value</code></em> is given,
the table attributes are unset.
</p><p>
In <code class="literal">latex-longtable</code> format, this controls
the proportional width of each column containing a left-aligned
data type. It is specified as a whitespace-separated list of values,
e.g., <code class="literal">'0.2 0.2 0.6'</code>. Unspecified output columns
use the last specified value.
</p></dd><dt><span class="term"><code class="literal">title</code> (or <code class="literal">C</code>)</span></dt><dd><p>
Sets the table title for any subsequently printed tables. This
can be used to give your output descriptive tags. If no
<em class="replaceable"><code>value</code></em> is given,
the title is unset.
</p></dd><dt><span class="term"><code class="literal">tuples_only</code> (or <code class="literal">t</code>)</span></dt><dd><p>
If <em class="replaceable"><code>value</code></em> is specified
it must be either <code class="literal">on</code> or <code class="literal">off</code>
which will enable or disable tuples-only mode.
If <em class="replaceable"><code>value</code></em> is omitted the
command toggles between regular and tuples-only output.
Regular output includes extra information such
as column headers, titles, and various footers. In tuples-only
mode, only actual table data is shown.
</p></dd><dt><span class="term"><code class="literal">unicode_border_linestyle</code></span></dt><dd><p>
Sets the border drawing style for the <code class="literal">unicode</code>
line style to one of <code class="literal">single</code>
or <code class="literal">double</code>.
</p></dd><dt><span class="term"><code class="literal">unicode_column_linestyle</code></span></dt><dd><p>
Sets the column drawing style for the <code class="literal">unicode</code>
line style to one of <code class="literal">single</code>
or <code class="literal">double</code>.
</p></dd><dt><span class="term"><code class="literal">unicode_header_linestyle</code></span></dt><dd><p>
Sets the header drawing style for the <code class="literal">unicode</code>
line style to one of <code class="literal">single</code>
or <code class="literal">double</code>.
</p></dd></dl></div><p>
</p><p>
Illustrations of how these different formats look can be seen in
<a class="xref" href="app-psql.html#APP-PSQL-EXAMPLES" title="Examples">Examples</a>, below.
</p><div class="tip"><h3 class="title">Tip</h3><p>
There are various shortcut commands for <code class="command">\pset</code>. See
<code class="command">\a</code>, <code class="command">\C</code>, <code class="command">\f</code>,
<code class="command">\H</code>, <code class="command">\t</code>, <code class="command">\T</code>,
and <code class="command">\x</code>.
</p></div></dd><dt><span class="term"><code class="literal">\q</code> or <code class="literal">\quit</code></span></dt><dd><p>
Quits the <span class="application">psql</span> program.
In a script file, only execution of that script is terminated.
</p></dd><dt><span class="term"><code class="literal">\qecho <em class="replaceable"><code>text</code></em> [ ... ] </code></span></dt><dd><p>
This command is identical to <code class="command">\echo</code> except
that the output will be written to the query output channel, as
set by <code class="command">\o</code>.
</p></dd><dt><span class="term"><code class="literal">\r</code> or <code class="literal">\reset</code></span></dt><dd><p>
Resets (clears) the query buffer.
</p></dd><dt><span class="term"><code class="literal">\s [ <em class="replaceable"><code>filename</code></em> ]</code></span></dt><dd><p>
Print <span class="application">psql</span>'s command line history
to <em class="replaceable"><code>filename</code></em>.
If <em class="replaceable"><code>filename</code></em> is omitted,
the history is written to the standard output (using the pager if
appropriate). This command is not available
if <span class="application">psql</span> was built
without <span class="application">Readline</span> support.
</p></dd><dt><span class="term"><code class="literal">\set [ <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> [ ... ] ] ]</code></span></dt><dd><p>
Sets the <span class="application">psql</span> variable <em class="replaceable"><code>name</code></em> to <em class="replaceable"><code>value</code></em>, or if more than one value
is given, to the concatenation of all of them. If only one
argument is given, the variable is set to an empty-string value. To
unset a variable, use the <code class="command">\unset</code> command.
</p><p><code class="command">\set</code> without any arguments displays the names and values
of all currently-set <span class="application">psql</span> variables.
</p><p>
Valid variable names can contain letters, digits, and
underscores. See <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below for details.
Variable names are case-sensitive.
</p><p>
Certain variables are special, in that they
control <span class="application">psql</span>'s behavior or are
automatically set to reflect connection state. These variables are
documented in <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, below.
</p><div class="note"><h3 class="title">Note</h3><p>
This command is unrelated to the <acronym class="acronym">SQL</acronym>
command <a class="link" href="sql-set.html" title="SET"><code class="command">SET</code></a>.
</p></div></dd><dt><span class="term"><code class="literal">\setenv <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> ]</code></span></dt><dd><p>
Sets the environment variable <em class="replaceable"><code>name</code></em> to <em class="replaceable"><code>value</code></em>, or if the
<em class="replaceable"><code>value</code></em> is
not supplied, unsets the environment variable. Example:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\setenv PAGER less</code></strong>
testdb=> <strong class="userinput"><code>\setenv LESS -imx4F</code></strong>
</pre></dd><dt><span class="term"><code class="literal">\sf[+] <em class="replaceable"><code>function_description</code></em> </code></span></dt><dd><p>
This command fetches and shows the definition of the named function or procedure,
in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
<code class="command">CREATE OR REPLACE PROCEDURE</code> command.
The definition is printed to the current query output channel,
as set by <code class="command">\o</code>.
</p><p>
The target function can be specified by name alone, or by name
and arguments, for example <code class="literal">foo(integer, text)</code>.
The argument types must be given if there is more
than one function of the same name.
</p><p>
If <code class="literal">+</code> is appended to the command name, then the
output lines are numbered, with the first line of the function body
being line 1.
</p><p>
Unlike most other meta-commands, the entire remainder of the line is
always taken to be the argument(s) of <code class="command">\sf</code>, and neither
variable interpolation nor backquote expansion are performed in the
arguments.
</p></dd><dt><span class="term"><code class="literal">\sv[+] <em class="replaceable"><code>view_name</code></em> </code></span></dt><dd><p>
This command fetches and shows the definition of the named view,
in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
The definition is printed to the current query output channel,
as set by <code class="command">\o</code>.
</p><p>
If <code class="literal">+</code> is appended to the command name, then the
output lines are numbered from 1.
</p><p>
Unlike most other meta-commands, the entire remainder of the line is
always taken to be the argument(s) of <code class="command">\sv</code>, and neither
variable interpolation nor backquote expansion are performed in the
arguments.
</p></dd><dt><span class="term"><code class="literal">\t</code></span></dt><dd><p>
Toggles the display of output column name headings and row count
footer. This command is equivalent to <code class="literal">\pset
tuples_only</code> and is provided for convenience.
</p></dd><dt><span class="term"><code class="literal">\T <em class="replaceable"><code>table_options</code></em></code></span></dt><dd><p>
Specifies attributes to be placed within the
<code class="sgmltag-element">table</code> tag in <acronym class="acronym">HTML</acronym>
output format. This command is equivalent to <code class="literal">\pset
tableattr <em class="replaceable"><code>table_options</code></em></code>.
</p></dd><dt><span class="term"><code class="literal">\timing [ <em class="replaceable"><code>on</code></em> | <em class="replaceable"><code>off</code></em> ]</code></span></dt><dd><p>
With a parameter, turns displaying of how long each SQL statement
takes on or off. Without a parameter, toggles the display between
on and off. The display is in milliseconds; intervals longer than
1 second are also shown in minutes:seconds format, with hours and
days fields added if needed.
</p></dd><dt><span class="term"><code class="literal">\unset <em class="replaceable"><code>name</code></em></code></span></dt><dd><p>
Unsets (deletes) the <span class="application">psql</span> variable <em class="replaceable"><code>name</code></em>.
</p><p>
Most variables that control <span class="application">psql</span>'s behavior
cannot be unset; instead, an <code class="literal">\unset</code> command is interpreted
as setting them to their default values.
See <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below.
</p></dd><dt><span class="term"><code class="literal">\w</code> or <code class="literal">\write</code> <em class="replaceable"><code>filename</code></em><br /></span><span class="term"><code class="literal">\w</code> or <code class="literal">\write</code> <code class="literal">|</code><em class="replaceable"><code>command</code></em></span></dt><dd><p>
Writes the current query buffer to the file <em class="replaceable"><code>filename</code></em> or pipes it to the shell
command <em class="replaceable"><code>command</code></em>.
If the current query buffer is empty, the most recently executed query
is written instead.
</p><p>
If the argument begins with <code class="literal">|</code>, then the entire remainder
of the line is taken to be
the <em class="replaceable"><code>command</code></em> to execute,
and neither variable interpolation nor backquote expansion are
performed in it. The rest of the line is simply passed literally to
the shell.
</p></dd><dt><span class="term"><code class="literal">\warn <em class="replaceable"><code>text</code></em> [ ... ]</code></span></dt><dd><p>
This command is identical to <code class="command">\echo</code> except
that the output will be written to <span class="application">psql</span>'s
standard error channel, rather than standard output.
</p></dd><dt><span class="term"><code class="literal">\watch [ <em class="replaceable"><code>seconds</code></em> ]</code></span></dt><dd><p>
Repeatedly execute the current query buffer (as <code class="literal">\g</code> does)
until interrupted or the query fails. Wait the specified number of
seconds (default 2) between executions. Each query result is
displayed with a header that includes the <code class="literal">\pset title</code>
string (if any), the time as of query start, and the delay interval.
</p><p>
If the current query buffer is empty, the most recently sent query
is re-executed instead.
</p></dd><dt><span class="term"><code class="literal">\x [ <em class="replaceable"><code>on</code></em> | <em class="replaceable"><code>off</code></em> | <em class="replaceable"><code>auto</code></em> ]</code></span></dt><dd><p>
Sets or toggles expanded table formatting mode. As such it is equivalent to
<code class="literal">\pset expanded</code>.
</p></dd><dt><span class="term"><code class="literal">\z [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
Lists tables, views and sequences with their
associated access privileges.
If a <em class="replaceable"><code>pattern</code></em> is
specified, only tables, views and sequences whose names match the
pattern are listed.
</p><p>
This is an alias for <code class="command">\dp</code> (<span class="quote">“<span class="quote">display
privileges</span>”</span>).
</p></dd><dt><span class="term"><code class="literal">\! [ <em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
With no argument, escapes to a sub-shell; <span class="application">psql</span>
resumes when the sub-shell exits. With an argument, executes the
shell command <em class="replaceable"><code>command</code></em>.
</p><p>
Unlike most other meta-commands, the entire remainder of the line is
always taken to be the argument(s) of <code class="command">\!</code>, and neither
variable interpolation nor backquote expansion are performed in the
arguments. The rest of the line is simply passed literally to the
shell.
</p></dd><dt><span class="term"><code class="literal">\? [ <em class="replaceable"><code>topic</code></em> ]</code></span></dt><dd><p>
Shows help information. The optional
<em class="replaceable"><code>topic</code></em> parameter
(defaulting to <code class="literal">commands</code>) selects which part of <span class="application">psql</span> is
explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
backslash commands; <code class="literal">options</code> describes the command-line
options that can be passed to <span class="application">psql</span>;
and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
variables.
</p></dd><dt><span class="term"><code class="literal">\;</code></span></dt><dd><p>
Backslash-semicolon is not a meta-command in the same way as the
preceding commands; rather, it simply causes a semicolon to be
added to the query buffer without any further processing.
</p><p>
Normally, <span class="application">psql</span> will dispatch an SQL command to the
server as soon as it reaches the command-ending semicolon, even if
more input remains on the current line. Thus for example entering
</p><pre class="programlisting">
select 1; select 2; select 3;
</pre><p>
will result in the three SQL commands being individually sent to
the server, with each one's results being displayed before
continuing to the next command. However, a semicolon entered
as <code class="literal">\;</code> will not trigger command processing, so that the
command before it and the one after are effectively combined and
sent to the server in one request. So for example
</p><pre class="programlisting">
select 1\; select 2\; select 3;
</pre><p>
results in sending the three SQL commands to the server in a single
request, when the non-backslashed semicolon is reached.
The server executes such a request as a single transaction,
unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
commands included in the string to divide it into multiple
transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="53.2.2.1. Multiple Statements in a Simple Query">Section 53.2.2.1</a>
for more details about how the server handles multi-query strings.)
<span class="application">psql</span> prints only the last query result
it receives for each request; in this example, although all
three <code class="command">SELECT</code>s are indeed executed, <span class="application">psql</span>
only prints the <code class="literal">3</code>.
</p></dd></dl></div><p>
</p><div class="refsect3" id="APP-PSQL-PATTERNS"><h4>Patterns</h4><a id="id-1.9.4.20.8.4.11.2" class="indexterm"></a><p>
The various <code class="literal">\d</code> commands accept a <em class="replaceable"><code>pattern</code></em> parameter to specify the
object name(s) to be displayed. In the simplest case, a pattern
is just the exact name of the object. The characters within a
pattern are normally folded to lower case, just as in SQL names;
for example, <code class="literal">\dt FOO</code> will display the table named
<code class="literal">foo</code>. As in SQL names, placing double quotes around
a pattern stops folding to lower case. Should you need to include
an actual double quote character in a pattern, write it as a pair
of double quotes within a double-quote sequence; again this is in
accord with the rules for SQL quoted identifiers. For example,
<code class="literal">\dt "FOO""BAR"</code> will display the table named
<code class="literal">FOO"BAR</code> (not <code class="literal">foo"bar</code>). Unlike the normal
rules for SQL names, you can put double quotes around just part
of a pattern, for instance <code class="literal">\dt FOO"FOO"BAR</code> will display
the table named <code class="literal">fooFOObar</code>.
</p><p>
Whenever the <em class="replaceable"><code>pattern</code></em> parameter
is omitted completely, the <code class="literal">\d</code> commands display all objects
that are visible in the current schema search path — this is
equivalent to using <code class="literal">*</code> as the pattern.
(An object is said to be <em class="firstterm">visible</em> if its
containing schema is in the search path and no object of the same
kind and name appears earlier in the search path. This is equivalent to the
statement that the object can be referenced by name without explicit
schema qualification.)
To see all objects in the database regardless of visibility,
use <code class="literal">*.*</code> as the pattern.
</p><p>
Within a pattern, <code class="literal">*</code> matches any sequence of characters
(including no characters) and <code class="literal">?</code> matches any single character.
(This notation is comparable to Unix shell file name patterns.)
For example, <code class="literal">\dt int*</code> displays tables whose names
begin with <code class="literal">int</code>. But within double quotes, <code class="literal">*</code>
and <code class="literal">?</code> lose these special meanings and are just matched
literally.
</p><p>
A relation pattern that contains a dot (<code class="literal">.</code>) is interpreted as a schema
name pattern followed by an object name pattern. For example,
<code class="literal">\dt foo*.*bar*</code> displays all tables whose table name
includes <code class="literal">bar</code> that are in schemas whose schema name
starts with <code class="literal">foo</code>. When no dot appears, then the pattern
matches only objects that are visible in the current schema search path.
Again, a dot within double quotes loses its special meaning and is matched
literally. A relation pattern that contains two dots (<code class="literal">.</code>)
is interpreted as a database name followed by a schema name pattern followed
by an object name pattern. The database name portion will not be treated as
a pattern and must match the name of the currently connected database, else
an error will be raised.
</p><p>
A schema pattern that contains a dot (<code class="literal">.</code>) is interpreted
as a database name followed by a schema name pattern. For example,
<code class="literal">\dn mydb.*foo*</code> displays all schemas whose schema name
includes <code class="literal">foo</code>. The database name portion will not be
treated as a pattern and must match the name of the currently connected
database, else an error will be raised.
</p><p>
Advanced users can use regular-expression notations such as character
classes, for example <code class="literal">[0-9]</code> to match any digit. All regular
expression special characters work as specified in
<a class="xref" href="functions-matching.html#FUNCTIONS-POSIX-REGEXP" title="9.7.3. POSIX Regular Expressions">Section 9.7.3</a>, except for <code class="literal">.</code> which
is taken as a separator as mentioned above, <code class="literal">*</code> which is
translated to the regular-expression notation <code class="literal">.*</code>,
<code class="literal">?</code> which is translated to <code class="literal">.</code>, and
<code class="literal">$</code> which is matched literally. You can emulate
these pattern characters at need by writing
<code class="literal">?</code> for <code class="literal">.</code>,
<code class="literal">(<em class="replaceable"><code>R</code></em>+|)</code> for
<code class="literal"><em class="replaceable"><code>R</code></em>*</code>, or
<code class="literal">(<em class="replaceable"><code>R</code></em>|)</code> for
<code class="literal"><em class="replaceable"><code>R</code></em>?</code>.
<code class="literal">$</code> is not needed as a regular-expression character since
the pattern must match the whole name, unlike the usual
interpretation of regular expressions (in other words, <code class="literal">$</code>
is automatically appended to your pattern). Write <code class="literal">*</code> at the
beginning and/or end if you don't wish the pattern to be anchored.
Note that within double quotes, all regular expression special characters
lose their special meanings and are matched literally. Also, the regular
expression special characters are matched literally in operator name
patterns (i.e., the argument of <code class="literal">\do</code>).
</p></div></div><div class="refsect2" id="id-1.9.4.20.8.5"><h3>Advanced Features</h3><div class="refsect3" id="APP-PSQL-VARIABLES"><h4>Variables</h4><p>
<span class="application">psql</span> provides variable substitution
features similar to common Unix command shells.
Variables are simply name/value pairs, where the value
can be any string of any length. The name must consist of letters
(including non-Latin letters), digits, and underscores.
</p><p>
To set a variable, use the <span class="application">psql</span> meta-command
<code class="command">\set</code>. For example,
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\set foo bar</code></strong>
</pre><p>
sets the variable <code class="literal">foo</code> to the value
<code class="literal">bar</code>. To retrieve the content of the variable, precede
the name with a colon, for example:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\echo :foo</code></strong>
bar
</pre><p>
This works in both regular SQL commands and meta-commands; there is
more detail in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation">SQL Interpolation</a>, below.
</p><p>
If you call <code class="command">\set</code> without a second argument, the
variable is set to an empty-string value. To unset (i.e., delete)
a variable, use the command <code class="command">\unset</code>. To show the
values of all variables, call <code class="command">\set</code> without any argument.
</p><div class="note"><h3 class="title">Note</h3><p>
The arguments of <code class="command">\set</code> are subject to the same
substitution rules as with other commands. Thus you can construct
interesting references such as <code class="literal">\set :foo
'something'</code> and get <span class="quote">“<span class="quote">soft links</span>”</span> or
<span class="quote">“<span class="quote">variable variables</span>”</span> of <span class="productname">Perl</span>
or <span class="productname"><acronym class="acronym">PHP</acronym></span> fame,
respectively. Unfortunately (or fortunately?), there is no way to do
anything useful with these constructs. On the other hand,
<code class="literal">\set bar :foo</code> is a perfectly valid way to copy a
variable.
</p></div><p>
A number of these variables are treated specially
by <span class="application">psql</span>. They represent certain option
settings that can be changed at run time by altering the value of
the variable, or in some cases represent changeable state of
<span class="application">psql</span>.
By convention, all specially treated variables' names
consist of all upper-case ASCII letters (and possibly digits and
underscores). To ensure maximum compatibility in the future, avoid
using such variable names for your own purposes.
</p><p>
Variables that control <span class="application">psql</span>'s behavior
generally cannot be unset or set to invalid values. An <code class="literal">\unset</code>
command is allowed but is interpreted as setting the variable to its
default value. A <code class="literal">\set</code> command without a second argument is
interpreted as setting the variable to <code class="literal">on</code>, for control
variables that accept that value, and is rejected for others. Also,
control variables that accept the values <code class="literal">on</code>
and <code class="literal">off</code> will also accept other common spellings of Boolean
values, such as <code class="literal">true</code> and <code class="literal">false</code>.
</p><p>
The specially treated variables are:
</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">
<code class="varname">AUTOCOMMIT</code>
<a id="id-1.9.4.20.8.5.2.9.1.1.2" class="indexterm"></a>
</span></dt><dd><p>
When <code class="literal">on</code> (the default), each SQL command is automatically
committed upon successful completion. To postpone commit in this
mode, you must enter a <code class="command">BEGIN</code> or <code class="command">START
TRANSACTION</code> SQL command. When <code class="literal">off</code> or unset, SQL
commands are not committed until you explicitly issue
<code class="command">COMMIT</code> or <code class="command">END</code>. The autocommit-off
mode works by issuing an implicit <code class="command">BEGIN</code> for you, just
before any command that is not already in a transaction block and
is not itself a <code class="command">BEGIN</code> or other transaction-control
command, nor a command that cannot be executed inside a transaction
block (such as <code class="command">VACUUM</code>).
</p><div class="note"><h3 class="title">Note</h3><p>
In autocommit-off mode, you must explicitly abandon any failed
transaction by entering <code class="command">ABORT</code> or <code class="command">ROLLBACK</code>.
Also keep in mind that if you exit the session
without committing, your work will be lost.
</p></div><div class="note"><h3 class="title">Note</h3><p>
The autocommit-on mode is <span class="productname">PostgreSQL</span>'s traditional
behavior, but autocommit-off is closer to the SQL spec. If you
prefer autocommit-off, you might wish to set it in the system-wide
<code class="filename">psqlrc</code> file or your
<code class="filename">~/.psqlrc</code> file.
</p></div></dd><dt><span class="term"><code class="varname">COMP_KEYWORD_CASE</code></span></dt><dd><p>
Determines which letter case to use when completing an SQL key word.
If set to <code class="literal">lower</code> or <code class="literal">upper</code>, the
completed word will be in lower or upper case, respectively. If set
to <code class="literal">preserve-lower</code>
or <code class="literal">preserve-upper</code> (the default), the completed word
will be in the case of the word already entered, but words being
completed without anything entered will be in lower or upper case,
respectively.
</p></dd><dt><span class="term"><code class="varname">DBNAME</code></span></dt><dd><p>
The name of the database you are currently connected to. This is
set every time you connect to a database (including program
start-up), but can be changed or unset.
</p></dd><dt><span class="term"><code class="varname">ECHO</code></span></dt><dd><p>
If set to <code class="literal">all</code>, all nonempty input lines are printed
to standard output as they are read. (This does not apply to lines
read interactively.) To select this behavior on program
start-up, use the switch <code class="option">-a</code>. If set to
<code class="literal">queries</code>,
<span class="application">psql</span> prints each query to standard output
as it is sent to the server. The switch to select this behavior is
<code class="option">-e</code>. If set to <code class="literal">errors</code>, then only
failed queries are displayed on standard error output. The switch
for this behavior is <code class="option">-b</code>. If set to
<code class="literal">none</code> (the default), then no queries are displayed.
</p></dd><dt><span class="term"><code class="varname">ECHO_HIDDEN</code></span></dt><dd><p>
When this variable is set to <code class="literal">on</code> and a backslash command
queries the database, the query is first shown.
This feature helps you to study
<span class="productname">PostgreSQL</span> internals and provide
similar functionality in your own programs. (To select this behavior
on program start-up, use the switch <code class="option">-E</code>.) If you set
this variable to the value <code class="literal">noexec</code>, the queries are
just shown but are not actually sent to the server and executed.
The default value is <code class="literal">off</code>.
</p></dd><dt><span class="term"><code class="varname">ENCODING</code></span></dt><dd><p>
The current client character set encoding.
This is set every time you connect to a database (including
program start-up), and when you change the encoding
with <code class="literal">\encoding</code>, but it can be changed or unset.
</p></dd><dt><span class="term"><code class="varname">ERROR</code></span></dt><dd><p>
<code class="literal">true</code> if the last SQL query failed, <code class="literal">false</code> if
it succeeded. See also <code class="varname">SQLSTATE</code>.
</p></dd><dt><span class="term"><code class="varname">FETCH_COUNT</code></span></dt><dd><p>
If this variable is set to an integer value greater than zero,
the results of <code class="command">SELECT</code> queries are fetched
and displayed in groups of that many rows, rather than the
default behavior of collecting the entire result set before
display. Therefore only a
limited amount of memory is used, regardless of the size of
the result set. Settings of 100 to 1000 are commonly used
when enabling this feature.
Keep in mind that when using this feature, a query might
fail after having already displayed some rows.
</p><div class="tip"><h3 class="title">Tip</h3><p>
Although you can use any output format with this feature,
the default <code class="literal">aligned</code> format tends to look bad
because each group of <code class="varname">FETCH_COUNT</code> rows
will be formatted separately, leading to varying column
widths across the row groups. The other output formats work better.
</p></div></dd><dt><span class="term"><code class="varname">HIDE_TABLEAM</code></span></dt><dd><p>
If this variable is set to <code class="literal">true</code>, a table's access
method details are not displayed. This is mainly useful for
regression tests.
</p></dd><dt><span class="term"><code class="varname">HIDE_TOAST_COMPRESSION</code></span></dt><dd><p>
If this variable is set to <code class="literal">true</code>, column
compression method details are not displayed. This is mainly
useful for regression tests.
</p></dd><dt><span class="term"><code class="varname">HISTCONTROL</code></span></dt><dd><p>
If this variable is set to <code class="literal">ignorespace</code>,
lines which begin with a space are not entered into the history
list. If set to a value of <code class="literal">ignoredups</code>, lines
matching the previous history line are not entered. A value of
<code class="literal">ignoreboth</code> combines the two options. If
set to <code class="literal">none</code> (the default), all lines
read in interactive mode are saved on the history list.
</p><div class="note"><h3 class="title">Note</h3><p>
This feature was shamelessly plagiarized from
<span class="application">Bash</span>.
</p></div></dd><dt><span class="term"><code class="varname">HISTFILE</code></span></dt><dd><p>
The file name that will be used to store the history list. If unset,
the file name is taken from the <code class="envar">PSQL_HISTORY</code>
environment variable. If that is not set either, the default
is <code class="filename">~/.psql_history</code>,
or <code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
For example, putting:
</p><pre class="programlisting">
\set HISTFILE ~/.psql_history-:DBNAME
</pre><p>
in <code class="filename">~/.psqlrc</code> will cause
<span class="application">psql</span> to maintain a separate history for
each database.
</p><div class="note"><h3 class="title">Note</h3><p>
This feature was shamelessly plagiarized from
<span class="application">Bash</span>.
</p></div></dd><dt><span class="term"><code class="varname">HISTSIZE</code></span></dt><dd><p>
The maximum number of commands to store in the command history
(default 500). If set to a negative value, no limit is applied.
</p><div class="note"><h3 class="title">Note</h3><p>
This feature was shamelessly plagiarized from
<span class="application">Bash</span>.
</p></div></dd><dt><span class="term"><code class="varname">HOST</code></span></dt><dd><p>
The database server host you are currently connected to. This is
set every time you connect to a database (including program
start-up), but can be changed or unset.
</p></dd><dt><span class="term"><code class="varname">IGNOREEOF</code></span></dt><dd><p>
If set to 1 or less, sending an <acronym class="acronym">EOF</acronym> character (usually
<span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>D</strong></span>)
to an interactive session of <span class="application">psql</span>
will terminate the application. If set to a larger numeric value,
that many consecutive <acronym class="acronym">EOF</acronym> characters must be typed to
make an interactive session terminate. If the variable is set to a
non-numeric value, it is interpreted as 10. The default is 0.
</p><div class="note"><h3 class="title">Note</h3><p>
This feature was shamelessly plagiarized from
<span class="application">Bash</span>.
</p></div></dd><dt><span class="term"><code class="varname">LASTOID</code></span></dt><dd><p>
The value of the last affected OID, as returned from an
<code class="command">INSERT</code> or <code class="command">\lo_import</code>
command. This variable is only guaranteed to be valid until
after the result of the next <acronym class="acronym">SQL</acronym> command has
been displayed.
<span class="productname">PostgreSQL</span> servers since version 12 do not
support OID system columns anymore, thus LASTOID will always be 0
following <code class="command">INSERT</code> when targeting such servers.
</p></dd><dt><span class="term"><code class="varname">LAST_ERROR_MESSAGE</code><br /></span><span class="term"><code class="varname">LAST_ERROR_SQLSTATE</code></span></dt><dd><p>
The primary error message and associated SQLSTATE code for the most
recent failed query in the current <span class="application">psql</span> session, or
an empty string and <code class="literal">00000</code> if no error has occurred in
the current session.
</p></dd><dt><span class="term">
<code class="varname">ON_ERROR_ROLLBACK</code>
<a id="id-1.9.4.20.8.5.2.9.18.1.2" class="indexterm"></a>
</span></dt><dd><p>
When set to <code class="literal">on</code>, if a statement in a transaction block
generates an error, the error is ignored and the transaction
continues. When set to <code class="literal">interactive</code>, such errors are only
ignored in interactive sessions, and not when reading script
files. When set to <code class="literal">off</code> (the default), a statement in a
transaction block that generates an error aborts the entire
transaction. The error rollback mode works by issuing an
implicit <code class="command">SAVEPOINT</code> for you, just before each command
that is in a transaction block, and then rolling back to the
savepoint if the command fails.
</p></dd><dt><span class="term"><code class="varname">ON_ERROR_STOP</code></span></dt><dd><p>
By default, command processing continues after an error. When this
variable is set to <code class="literal">on</code>, processing will instead stop
immediately. In interactive mode,
<span class="application">psql</span> will return to the command prompt;
otherwise, <span class="application">psql</span> will exit, returning
error code 3 to distinguish this case from fatal error
conditions, which are reported using error code 1. In either case,
any currently running scripts (the top-level script, if any, and any
other scripts which it may have in invoked) will be terminated
immediately. If the top-level command string contained multiple SQL
commands, processing will stop with the current command.
</p></dd><dt><span class="term"><code class="varname">PORT</code></span></dt><dd><p>
The database server port to which you are currently connected.
This is set every time you connect to a database (including
program start-up), but can be changed or unset.
</p></dd><dt><span class="term"><code class="varname">PROMPT1</code><br /></span><span class="term"><code class="varname">PROMPT2</code><br /></span><span class="term"><code class="varname">PROMPT3</code></span></dt><dd><p>
These specify what the prompts <span class="application">psql</span>
issues should look like. See <a class="xref" href="app-psql.html#APP-PSQL-PROMPTING" title="Prompting">Prompting</a> below.
</p></dd><dt><span class="term"><code class="varname">QUIET</code></span></dt><dd><p>
Setting this variable to <code class="literal">on</code> is equivalent to the command
line option <code class="option">-q</code>. It is probably not too useful in
interactive mode.
</p></dd><dt><span class="term"><code class="varname">ROW_COUNT</code></span></dt><dd><p>
The number of rows returned or affected by the last SQL query, or 0
if the query failed or did not report a row count.
</p></dd><dt><span class="term"><code class="varname">SERVER_VERSION_NAME</code><br /></span><span class="term"><code class="varname">SERVER_VERSION_NUM</code></span></dt><dd><p>
The server's version number as a string, for
example <code class="literal">9.6.2</code>, <code class="literal">10.1</code> or <code class="literal">11beta1</code>,
and in numeric form, for
example <code class="literal">90602</code> or <code class="literal">100001</code>.
These are set every time you connect to a database
(including program start-up), but can be changed or unset.
</p></dd><dt><span class="term"><code class="varname">SHOW_CONTEXT</code></span></dt><dd><p>
This variable can be set to the
values <code class="literal">never</code>, <code class="literal">errors</code>, or <code class="literal">always</code>
to control whether <code class="literal">CONTEXT</code> fields are displayed in
messages from the server. The default is <code class="literal">errors</code> (meaning
that context will be shown in error messages, but not in notice or
warning messages). This setting has no effect
when <code class="varname">VERBOSITY</code> is set to <code class="literal">terse</code>
or <code class="literal">sqlstate</code>.
(See also <code class="command">\errverbose</code>, for use when you want a verbose
version of the error you just got.)
</p></dd><dt><span class="term"><code class="varname">SINGLELINE</code></span></dt><dd><p>
Setting this variable to <code class="literal">on</code> is equivalent to the command
line option <code class="option">-S</code>.
</p></dd><dt><span class="term"><code class="varname">SINGLESTEP</code></span></dt><dd><p>
Setting this variable to <code class="literal">on</code> is equivalent to the command
line option <code class="option">-s</code>.
</p></dd><dt><span class="term"><code class="varname">SQLSTATE</code></span></dt><dd><p>
The error code (see <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>) associated
with the last SQL query's failure, or <code class="literal">00000</code> if it
succeeded.
</p></dd><dt><span class="term"><code class="varname">USER</code></span></dt><dd><p>
The database user you are currently connected as. This is set
every time you connect to a database (including program
start-up), but can be changed or unset.
</p></dd><dt><span class="term"><code class="varname">VERBOSITY</code></span></dt><dd><p>
This variable can be set to the values <code class="literal">default</code>,
<code class="literal">verbose</code>, <code class="literal">terse</code>,
or <code class="literal">sqlstate</code> to control the verbosity of error
reports.
(See also <code class="command">\errverbose</code>, for use when you want a verbose
version of the error you just got.)
</p></dd><dt><span class="term"><code class="varname">VERSION</code><br /></span><span class="term"><code class="varname">VERSION_NAME</code><br /></span><span class="term"><code class="varname">VERSION_NUM</code></span></dt><dd><p>
These variables are set at program start-up to reflect
<span class="application">psql</span>'s version, respectively as a verbose string,
a short string (e.g., <code class="literal">9.6.2</code>, <code class="literal">10.1</code>,
or <code class="literal">11beta1</code>), and a number (e.g., <code class="literal">90602</code>
or <code class="literal">100001</code>). They can be changed or unset.
</p></dd></dl></div></div><div class="refsect3" id="APP-PSQL-INTERPOLATION"><h4><acronym class="acronym">SQL</acronym> Interpolation</h4><p>
A key feature of <span class="application">psql</span>
variables is that you can substitute (<span class="quote">“<span class="quote">interpolate</span>”</span>)
them into regular <acronym class="acronym">SQL</acronym> statements, as well as the
arguments of meta-commands. Furthermore,
<span class="application">psql</span> provides facilities for
ensuring that variable values used as SQL literals and identifiers are
properly quoted. The syntax for interpolating a value without
any quoting is to prepend the variable name with a colon
(<code class="literal">:</code>). For example,
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\set foo 'my_table'</code></strong>
testdb=> <strong class="userinput"><code>SELECT * FROM :foo;</code></strong>
</pre><p>
would query the table <code class="literal">my_table</code>. Note that this
may be unsafe: the value of the variable is copied literally, so it can
contain unbalanced quotes, or even backslash commands. You must make sure
that it makes sense where you put it.
</p><p>
When a value is to be used as an SQL literal or identifier, it is
safest to arrange for it to be quoted. To quote the value of
a variable as an SQL literal, write a colon followed by the variable
name in single quotes. To quote the value as an SQL identifier, write
a colon followed by the variable name in double quotes.
These constructs deal correctly with quotes and other special
characters embedded within the variable value.
The previous example would be more safely written this way:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\set foo 'my_table'</code></strong>
testdb=> <strong class="userinput"><code>SELECT * FROM :"foo";</code></strong>
</pre><p>
</p><p>
Variable interpolation will not be performed within quoted
<acronym class="acronym">SQL</acronym> literals and identifiers. Therefore, a
construction such as <code class="literal">':foo'</code> doesn't work to produce a quoted
literal from a variable's value (and it would be unsafe if it did work,
since it wouldn't correctly handle quotes embedded in the value).
</p><p>
One example use of this mechanism is to
copy the contents of a file into a table column.
First load the file into a variable and then interpolate the variable's
value as a quoted string:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\set content `cat my_file.txt`</code></strong>
testdb=> <strong class="userinput"><code>INSERT INTO my_table VALUES (:'content');</code></strong>
</pre><p>
(Note that this still won't work if <code class="filename">my_file.txt</code> contains NUL bytes.
<span class="application">psql</span> does not support embedded NUL bytes in variable values.)
</p><p>
Since colons can legally appear in SQL commands, an apparent attempt
at interpolation (that is, <code class="literal">:name</code>,
<code class="literal">:'name'</code>, or <code class="literal">:"name"</code>) is not
replaced unless the named variable is currently set. In any case, you
can escape a colon with a backslash to protect it from substitution.
</p><p>
The <code class="literal">:{?<em class="replaceable"><code>name</code></em>}</code> special syntax returns TRUE
or FALSE depending on whether the variable exists or not, and is thus
always substituted, unless the colon is backslash-escaped.
</p><p>
The colon syntax for variables is standard <acronym class="acronym">SQL</acronym> for
embedded query languages, such as <span class="application">ECPG</span>.
The colon syntaxes for array slices and type casts are
<span class="productname">PostgreSQL</span> extensions, which can sometimes
conflict with the standard usage. The colon-quote syntax for escaping a
variable's value as an SQL literal or identifier is a
<span class="application">psql</span> extension.
</p></div><div class="refsect3" id="APP-PSQL-PROMPTING"><h4>Prompting</h4><p>
The prompts <span class="application">psql</span> issues can be customized
to your preference. The three variables <code class="varname">PROMPT1</code>,
<code class="varname">PROMPT2</code>, and <code class="varname">PROMPT3</code> contain strings
and special escape sequences that describe the appearance of the
prompt. Prompt 1 is the normal prompt that is issued when
<span class="application">psql</span> requests a new command. Prompt 2 is
issued when more input is expected during command entry, for example
because the command was not terminated with a semicolon or a quote
was not closed.
Prompt 3 is issued when you are running an <acronym class="acronym">SQL</acronym>
<code class="command">COPY FROM STDIN</code> command and you need to type in
a row value on the terminal.
</p><p>
The value of the selected prompt variable is printed literally,
except where a percent sign (<code class="literal">%</code>) is encountered.
Depending on the next character, certain other text is substituted
instead. Defined substitutions are:
</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">%M</code></span></dt><dd><p>
The full host name (with domain name) of the database server,
or <code class="literal">[local]</code> if the connection is over a Unix
domain socket, or
<code class="literal">[local:<em class="replaceable"><code>/dir/name</code></em>]</code>,
if the Unix domain socket is not at the compiled in default
location.
</p></dd><dt><span class="term"><code class="literal">%m</code></span></dt><dd><p>
The host name of the database server, truncated at the
first dot, or <code class="literal">[local]</code> if the connection is
over a Unix domain socket.
</p></dd><dt><span class="term"><code class="literal">%></code></span></dt><dd><p>The port number at which the database server is listening.</p></dd><dt><span class="term"><code class="literal">%n</code></span></dt><dd><p>
The database session user name. (The expansion of this
value might change during a database session as the result
of the command <code class="command">SET SESSION
AUTHORIZATION</code>.)
</p></dd><dt><span class="term"><code class="literal">%/</code></span></dt><dd><p>The name of the current database.</p></dd><dt><span class="term"><code class="literal">%~</code></span></dt><dd><p>Like <code class="literal">%/</code>, but the output is <code class="literal">~</code>
(tilde) if the database is your default database.</p></dd><dt><span class="term"><code class="literal">%#</code></span></dt><dd><p>
If the session user is a database superuser, then a
<code class="literal">#</code>, otherwise a <code class="literal">></code>.
(The expansion of this value might change during a database
session as the result of the command <code class="command">SET SESSION
AUTHORIZATION</code>.)
</p></dd><dt><span class="term"><code class="literal">%p</code></span></dt><dd><p>The process ID of the backend currently connected to.</p></dd><dt><span class="term"><code class="literal">%R</code></span></dt><dd><p>
In prompt 1 normally <code class="literal">=</code>,
but <code class="literal">@</code> if the session is in an inactive branch of a
conditional block, or <code class="literal">^</code> if in single-line mode,
or <code class="literal">!</code> if the session is disconnected from the
database (which can happen if <code class="command">\connect</code> fails).
In prompt 2 <code class="literal">%R</code> is replaced by a character that
depends on why <span class="application">psql</span> expects more input:
<code class="literal">-</code> if the command simply wasn't terminated yet,
but <code class="literal">*</code> if there is an unfinished
<code class="literal">/* ... */</code> comment,
a single quote if there is an unfinished quoted string,
a double quote if there is an unfinished quoted identifier,
a dollar sign if there is an unfinished dollar-quoted string,
or <code class="literal">(</code> if there is an unmatched left parenthesis.
In prompt 3 <code class="literal">%R</code> doesn't produce anything.
</p></dd><dt><span class="term"><code class="literal">%x</code></span></dt><dd><p>
Transaction status: an empty string when not in a transaction
block, or <code class="literal">*</code> when in a transaction block, or
<code class="literal">!</code> when in a failed transaction block, or <code class="literal">?</code>
when the transaction state is indeterminate (for example, because
there is no connection).
</p></dd><dt><span class="term"><code class="literal">%l</code></span></dt><dd><p>
The line number inside the current statement, starting from <code class="literal">1</code>.
</p></dd><dt><span class="term"><code class="literal">%</code><em class="replaceable"><code>digits</code></em></span></dt><dd><p>
The character with the indicated octal code is substituted.
</p></dd><dt><span class="term"><code class="literal">%:</code><em class="replaceable"><code>name</code></em><code class="literal">:</code></span></dt><dd><p>
The value of the <span class="application">psql</span> variable
<em class="replaceable"><code>name</code></em>. See
<a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, above, for details.
</p></dd><dt><span class="term"><code class="literal">%`</code><em class="replaceable"><code>command</code></em><code class="literal">`</code></span></dt><dd><p>
The output of <em class="replaceable"><code>command</code></em>, similar to ordinary
<span class="quote">“<span class="quote">back-tick</span>”</span> substitution.
</p></dd><dt><span class="term"><code class="literal">%[</code> ... <code class="literal">%]</code></span></dt><dd><p>
Prompts can contain terminal control characters which, for
example, change the color, background, or style of the prompt
text, or change the title of the terminal window. In order for
the line editing features of <span class="application">Readline</span> to work properly, these
non-printing control characters must be designated as invisible
by surrounding them with <code class="literal">%[</code> and
<code class="literal">%]</code>. Multiple pairs of these can occur within
the prompt. For example:
</p><pre class="programlisting">
testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
</pre><p>
results in a boldfaced (<code class="literal">1;</code>) yellow-on-black
(<code class="literal">33;40</code>) prompt on VT100-compatible, color-capable
terminals.
</p></dd><dt><span class="term"><code class="literal">%w</code></span></dt><dd><p>
Whitespace of the same width as the most recent output of
<code class="varname">PROMPT1</code>. This can be used as a
<code class="varname">PROMPT2</code> setting, so that multi-line statements are
aligned with the first line, but there is no visible secondary prompt.
</p></dd></dl></div><p>
To insert a percent sign into your prompt, write
<code class="literal">%%</code>. The default prompts are
<code class="literal">'%/%R%x%# '</code> for prompts 1 and 2, and
<code class="literal">'>> '</code> for prompt 3.
</p><div class="note"><h3 class="title">Note</h3><p>
This feature was shamelessly plagiarized from
<span class="application">tcsh</span>.
</p></div></div><div class="refsect3" id="id-1.9.4.20.8.5.5"><h4>Command-Line Editing</h4><p>
<span class="application">psql</span> supports the <span class="application">Readline</span>
library for convenient line editing and retrieval. The command
history is automatically saved when <span class="application">psql</span>
exits and is reloaded when
<span class="application">psql</span> starts up. Tab-completion is also
supported, although the completion logic makes no claim to be an
<acronym class="acronym">SQL</acronym> parser. The queries generated by tab-completion
can also interfere with other SQL commands, e.g., <code class="literal">SET
TRANSACTION ISOLATION LEVEL</code>.
If for some reason you do not like the tab completion, you
can turn it off by putting this in a file named
<code class="filename">.inputrc</code> in your home directory:
</p><pre class="programlisting">
$if psql
set disable-completion on
$endif
</pre><p>
(This is not a <span class="application">psql</span> but a
<span class="application">Readline</span> feature. Read its documentation
for further details.)
</p></div></div></div><div class="refsect1" id="APP-PSQL-ENVIRONMENT"><h2>Environment</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="envar">COLUMNS</code></span></dt><dd><p>
If <code class="literal">\pset columns</code> is zero, controls the
width for the <code class="literal">wrapped</code> format and width for determining
if wide output requires the pager or should be switched to the
vertical format in expanded auto mode.
</p></dd><dt><span class="term"><code class="envar">PGDATABASE</code><br /></span><span class="term"><code class="envar">PGHOST</code><br /></span><span class="term"><code class="envar">PGPORT</code><br /></span><span class="term"><code class="envar">PGUSER</code></span></dt><dd><p>
Default connection parameters (see <a class="xref" href="libpq-envars.html" title="34.15. Environment Variables">Section 34.15</a>).
</p></dd><dt><span class="term"><code class="envar">PG_COLOR</code></span></dt><dd><p>
Specifies whether to use color in diagnostic messages. Possible values
are <code class="literal">always</code>, <code class="literal">auto</code> and
<code class="literal">never</code>.
</p></dd><dt><span class="term"><code class="envar">PSQL_EDITOR</code><br /></span><span class="term"><code class="envar">EDITOR</code><br /></span><span class="term"><code class="envar">VISUAL</code></span></dt><dd><p>
Editor used by the <code class="command">\e</code>, <code class="command">\ef</code>,
and <code class="command">\ev</code> commands.
These variables are examined in the order listed;
the first that is set is used.
If none of them is set, the default is to use <code class="filename">vi</code>
on Unix systems or <code class="filename">notepad.exe</code> on Windows systems.
</p></dd><dt><span class="term"><code class="envar">PSQL_EDITOR_LINENUMBER_ARG</code></span></dt><dd><p>
When <code class="command">\e</code>, <code class="command">\ef</code>, or
<code class="command">\ev</code> is used
with a line number argument, this variable specifies the
command-line argument used to pass the starting line number to
the user's editor. For editors such as <span class="productname">Emacs</span> or
<span class="productname">vi</span>, this is a plus sign. Include a trailing
space in the value of the variable if there needs to be space
between the option name and the line number. Examples:
</p><pre class="programlisting">
PSQL_EDITOR_LINENUMBER_ARG='+'
PSQL_EDITOR_LINENUMBER_ARG='--line '
</pre><p>
</p><p>
The default is <code class="literal">+</code> on Unix systems
(corresponding to the default editor <code class="filename">vi</code>,
and useful for many other common editors); but there is no
default on Windows systems.
</p></dd><dt><span class="term"><code class="envar">PSQL_HISTORY</code></span></dt><dd><p>
Alternative location for the command history file. Tilde (<code class="literal">~</code>) expansion is performed.
</p></dd><dt><span class="term"><code class="envar">PSQL_PAGER</code><br /></span><span class="term"><code class="envar">PAGER</code></span></dt><dd><p>
If a query's results do not fit on the screen, they are piped
through this command. Typical values are <code class="literal">more</code>
or <code class="literal">less</code>.
Use of the pager can be disabled by setting <code class="envar">PSQL_PAGER</code>
or <code class="envar">PAGER</code> to an empty string, or by adjusting the
pager-related options of the <code class="command">\pset</code> command.
These variables are examined in the order listed;
the first that is set is used.
If none of them is set, the default is to use <code class="literal">more</code> on most
platforms, but <code class="literal">less</code> on Cygwin.
</p></dd><dt><span class="term"><code class="envar">PSQLRC</code></span></dt><dd><p>
Alternative location of the user's <code class="filename">.psqlrc</code> file. Tilde (<code class="literal">~</code>) expansion is performed.
</p></dd><dt><span class="term"><code class="envar">SHELL</code></span></dt><dd><p>
Command executed by the <code class="command">\!</code> command.
</p></dd><dt><span class="term"><code class="envar">TMPDIR</code></span></dt><dd><p>
Directory for storing temporary files. The default is
<code class="filename">/tmp</code>.
</p></dd></dl></div><p>
This utility, like most other <span class="productname">PostgreSQL</span> utilities,
also uses the environment variables supported by <span class="application">libpq</span>
(see <a class="xref" href="libpq-envars.html" title="34.15. Environment Variables">Section 34.15</a>).
</p></div><div class="refsect1" id="id-1.9.4.20.10"><h2>Files</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="filename">psqlrc</code> and <code class="filename">~/.psqlrc</code></span></dt><dd><p>
Unless it is passed an <code class="option">-X</code> option,
<span class="application">psql</span> attempts to read and execute commands
from the system-wide startup file (<code class="filename">psqlrc</code>) and then
the user's personal startup file (<code class="filename">~/.psqlrc</code>), after
connecting to the database but before accepting normal commands.
These files can be used to set up the client and/or the server to taste,
typically with <code class="command">\set</code> and <code class="command">SET</code>
commands.
</p><p>
The system-wide startup file is named <code class="filename">psqlrc</code> and is
sought in the installation's <span class="quote">“<span class="quote">system configuration</span>”</span> directory,
which is most reliably identified by running <code class="literal">pg_config
--sysconfdir</code>. By default this directory will be <code class="filename">../etc/</code>
relative to the directory containing
the <span class="productname">PostgreSQL</span> executables. The name of this
directory can be set explicitly via the <code class="envar">PGSYSCONFDIR</code>
environment variable.
</p><p>
The user's personal startup file is named <code class="filename">.psqlrc</code>
and is sought in the invoking user's home directory. On Windows, which
lacks such a concept, the personal startup file is named
<code class="filename">%APPDATA%\postgresql\psqlrc.conf</code>.
The location of the user's startup file can be set explicitly via
the <code class="envar">PSQLRC</code> environment variable.
</p><p>
Both the system-wide startup file and the user's personal startup file
can be made <span class="application">psql</span>-version-specific
by appending a dash and the <span class="productname">PostgreSQL</span>
major or minor release number to the file name,
for example <code class="filename">~/.psqlrc-9.2</code> or
<code class="filename">~/.psqlrc-9.2.5</code>. The most specific
version-matching file will be read in preference to a
non-version-specific file.
</p></dd><dt><span class="term"><code class="filename">.psql_history</code></span></dt><dd><p>
The command-line history is stored in the file
<code class="filename">~/.psql_history</code>, or
<code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
</p><p>
The location of the history file can be set explicitly via
the <code class="varname">HISTFILE</code> <span class="application">psql</span> variable or
the <code class="envar">PSQL_HISTORY</code> environment variable.
</p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.20.11"><h2>Notes</h2><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><span class="application">psql</span> works best with servers of the same
or an older major version. Backslash commands are particularly likely
to fail if the server is of a newer version than <span class="application">psql</span>
itself. However, backslash commands of the <code class="literal">\d</code> family should
work with servers of versions back to 7.4, though not necessarily with
servers newer than <span class="application">psql</span> itself. The general
functionality of running SQL commands and displaying query results
should also work with servers of a newer major version, but this cannot
be guaranteed in all cases.
</p><p>
If you want to use <span class="application">psql</span> to connect to several
servers of different major versions, it is recommended that you use the
newest version of <span class="application">psql</span>. Alternatively, you
can keep around a copy of <span class="application">psql</span> from each
major version and be sure to use the version that matches the
respective server. But in practice, this additional complication should
not be necessary.
</p></li><li class="listitem"><p>
Before <span class="productname">PostgreSQL</span> 9.6,
the <code class="option">-c</code> option implied <code class="option">-X</code>
(<code class="option">--no-psqlrc</code>); this is no longer the case.
</p></li><li class="listitem"><p>
Before <span class="productname">PostgreSQL</span> 8.4,
<span class="application">psql</span> allowed the
first argument of a single-letter backslash command to start
directly after the command, without intervening whitespace.
Now, some whitespace is required.
</p></li></ul></div></div><div class="refsect1" id="id-1.9.4.20.12"><h2>Notes for Windows Users</h2><p>
<span class="application">psql</span> is built as a <span class="quote">“<span class="quote">console
application</span>”</span>. Since the Windows console windows use a different
encoding than the rest of the system, you must take special care
when using 8-bit characters within <span class="application">psql</span>.
If <span class="application">psql</span> detects a problematic
console code page, it will warn you at startup. To change the
console code page, two things are necessary:
</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
Set the code page by entering <strong class="userinput"><code>cmd.exe /c chcp
1252</code></strong>. (1252 is a code page that is appropriate for
German; replace it with your value.) If you are using Cygwin,
you can put this command in <code class="filename">/etc/profile</code>.
</p></li><li class="listitem"><p>
Set the console font to <code class="literal">Lucida Console</code>, because the
raster font does not work with the ANSI code page.
</p></li></ul></div></div><div class="refsect1" id="APP-PSQL-EXAMPLES"><h2>Examples</h2><p>
The first example shows how to spread a command over several lines of
input. Notice the changing prompt:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>CREATE TABLE my_table (</code></strong>
testdb(> <strong class="userinput"><code> first integer not null default 0,</code></strong>
testdb(> <strong class="userinput"><code> second text)</code></strong>
testdb-> <strong class="userinput"><code>;</code></strong>
CREATE TABLE
</pre><p>
Now look at the table definition again:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\d my_table</code></strong>
Table "public.my_table"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
first | integer | | not null | 0
second | text | | |
</pre><p>
Now we change the prompt to something more interesting:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\set PROMPT1 '%n@%m %~%R%# '</code></strong>
peter@localhost testdb=>
</pre><p>
Let's assume you have filled the table with data and want to take a
look at it:
</p><pre class="programlisting">
peter@localhost testdb=> SELECT * FROM my_table;
first | second
-------+--------
1 | one
2 | two
3 | three
4 | four
(4 rows)
</pre><p>
You can display tables in different ways by using the
<code class="command">\pset</code> command:
</p><pre class="programlisting">
peter@localhost testdb=> <strong class="userinput"><code>\pset border 2</code></strong>
Border style is 2.
peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
+-------+--------+
| first | second |
+-------+--------+
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
+-------+--------+
(4 rows)
peter@localhost testdb=> <strong class="userinput"><code>\pset border 0</code></strong>
Border style is 0.
peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
first second
----- ------
1 one
2 two
3 three
4 four
(4 rows)
peter@localhost testdb=> <strong class="userinput"><code>\pset border 1</code></strong>
Border style is 1.
peter@localhost testdb=> <strong class="userinput"><code>\pset format csv</code></strong>
Output format is csv.
peter@localhost testdb=> <strong class="userinput"><code>\pset tuples_only</code></strong>
Tuples only is on.
peter@localhost testdb=> <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
one,1
two,2
three,3
four,4
peter@localhost testdb=> <strong class="userinput"><code>\pset format unaligned</code></strong>
Output format is unaligned.
peter@localhost testdb=> <strong class="userinput"><code>\pset fieldsep '\t'</code></strong>
Field separator is " ".
peter@localhost testdb=> <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
one 1
two 2
three 3
four 4
</pre><p>
Alternatively, use the short commands:
</p><pre class="programlisting">
peter@localhost testdb=> <strong class="userinput"><code>\a \t \x</code></strong>
Output format is aligned.
Tuples only is off.
Expanded display is on.
peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
-[ RECORD 1 ]-
first | 1
second | one
-[ RECORD 2 ]-
first | 2
second | two
-[ RECORD 3 ]-
first | 3
second | three
-[ RECORD 4 ]-
first | 4
second | four
</pre><p>
</p><p>
Also, these output format options can be set for just one query by using
<code class="literal">\g</code>:
</p><pre class="programlisting">
peter@localhost testdb=> <strong class="userinput"><code>SELECT * FROM my_table</code></strong>
peter@localhost testdb-> <strong class="userinput"><code>\g (format=aligned tuples_only=off expanded=on)</code></strong>
-[ RECORD 1 ]-
first | 1
second | one
-[ RECORD 2 ]-
first | 2
second | two
-[ RECORD 3 ]-
first | 3
second | three
-[ RECORD 4 ]-
first | 4
second | four
</pre><p>
</p><p>
Here is an example of using the <code class="command">\df</code> command to
find only functions with names matching <code class="literal">int*pl</code>
and whose second argument is of type <code class="type">bigint</code>:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>\df int*pl * bigint</code></strong>
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+---------+------------------+---------------------+------
pg_catalog | int28pl | bigint | smallint, bigint | func
pg_catalog | int48pl | bigint | integer, bigint | func
pg_catalog | int8pl | bigint | bigint, bigint | func
(3 rows)
</pre><p>
</p><p>
When suitable, query results can be shown in a crosstab representation
with the <code class="command">\crosstabview</code> command:
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>SELECT first, second, first > 2 AS gt2 FROM my_table;</code></strong>
first | second | gt2
-------+--------+-----
1 | one | f
2 | two | f
3 | three | t
4 | four | t
(4 rows)
testdb=> <strong class="userinput"><code>\crosstabview first second</code></strong>
first | one | two | three | four
-------+-----+-----+-------+------
1 | f | | |
2 | | f | |
3 | | | t |
4 | | | | t
(4 rows)
</pre><p>
This second example shows a multiplication table with rows sorted in reverse
numerical order and columns with an independent, ascending numerical order.
</p><pre class="programlisting">
testdb=> <strong class="userinput"><code>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</code></strong>
testdb(> <strong class="userinput"><code>row_number() over(order by t2.first) AS ord</code></strong>
testdb(> <strong class="userinput"><code>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</code></strong>
testdb(> <strong class="userinput"><code>\crosstabview "A" "B" "AxB" ord</code></strong>
A | 101 | 102 | 103 | 104
---+-----+-----+-----+-----
4 | 404 | 408 | 412 | 416
3 | 303 | 306 | 309 | 312
2 | 202 | 204 | 206 | 208
1 | 101 | 102 | 103 | 104
(4 rows)
</pre></div></div><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navfooter"><hr></hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgverifybackup.html" title="pg_verifybackup">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="app-reindexdb.html" title="reindexdb">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span xmlns="http://www.w3.org/1999/xhtml" class="application">pg_verifybackup</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 14.5 Documentation">Home</a></td><td width="40%" align="right" valign="top"> <span xmlns="http://www.w3.org/1999/xhtml" class="application">reindexdb</span></td></tr></table></div></body></html>
|