File: extras.html

package info (click to toggle)
psycopg2 2.5.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,160 kB
  • ctags: 3,036
  • sloc: ansic: 12,142; python: 9,384; makefile: 292; sh: 32
file content (1052 lines) | stat: -rw-r--r-- 92,314 bytes parent folder | download
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
<!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>psycopg2.extras – Miscellaneous goodies for Psycopg 2 &mdash; Psycopg 2.5.4 documentation</title>
    
    <link rel="stylesheet" href="_static/psycopg.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '2.5.4',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Psycopg 2.5.4 documentation" href="index.html" />
    <link rel="next" title="psycopg2.errorcodes – Error codes defined by PostgreSQL" href="errorcodes.html" />
    <link rel="prev" title="psycopg2.pool – Connections pooling" href="pool.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="errorcodes.html" title="psycopg2.errorcodes – Error codes defined by PostgreSQL"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pool.html" title="psycopg2.pool – Connections pooling"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Psycopg 2.5.4 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-psycopg2.extras">
<span id="psycopg2-extras-miscellaneous-goodies-for-psycopg-2"></span><h1><a class="reference internal" href="#module-psycopg2.extras" title="psycopg2.extras"><tt class="xref py py-obj docutils literal"><span class="pre">psycopg2.extras</span></tt></a> &#8211; Miscellaneous goodies for Psycopg 2<a class="headerlink" href="#module-psycopg2.extras" title="Permalink to this headline">¶</a></h1>
<p>This module is a generic place used to hold little helper functions and
classes until a better place in the distribution is found.</p>
<div class="section" id="connection-and-cursor-subclasses">
<span id="cursor-subclasses"></span><h2>Connection and cursor subclasses<a class="headerlink" href="#connection-and-cursor-subclasses" title="Permalink to this headline">¶</a></h2>
<p>A few objects that change the way the results are returned by the cursor or
modify the object behavior in some other way. Typically <tt class="xref py py-obj docutils literal"><span class="pre">cursor</span></tt> subclasses
are passed as <em>cursor_factory</em> argument to <a class="reference internal" href="module.html#psycopg2.connect" title="psycopg2.connect"><tt class="xref py py-obj docutils literal"><span class="pre">connect()</span></tt></a> so that the
connection&#8217;s <a class="reference internal" href="connection.html#connection.cursor" title="connection.cursor"><tt class="xref py py-obj docutils literal"><span class="pre">cursor()</span></tt></a> method will generate objects of this
class.  Alternatively a <tt class="xref py py-obj docutils literal"><span class="pre">cursor</span></tt> subclass can be used one-off by passing it
as the <em>cursor_factory</em> argument to the <tt class="xref py py-obj docutils literal"><span class="pre">cursor()</span></tt> method.</p>
<p>If you want to use a <tt class="xref py py-obj docutils literal"><span class="pre">connection</span></tt> subclass you can pass it as the
<em>connection_factory</em> argument of the <tt class="xref py py-obj docutils literal"><span class="pre">connect()</span></tt> function.</p>
<div class="section" id="dictionary-like-cursor">
<span id="dict-cursor"></span><span id="index-0"></span><h3>Dictionary-like cursor<a class="headerlink" href="#dictionary-like-cursor" title="Permalink to this headline">¶</a></h3>
<p>The dict cursors allow to access to the retrieved records using an interface
similar to the Python dictionaries instead of the tuples.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">dict_cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">(</span><span class="n">cursor_factory</span><span class="o">=</span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">DictCursor</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dict_cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;INSERT INTO test (num, data) VALUES(</span><span class="si">%s</span><span class="s">, </span><span class="si">%s</span><span class="s">)&quot;</span><span class="p">,</span>
<span class="gp">... </span>                 <span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="s">&quot;abc&#39;def&quot;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dict_cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT * FROM test&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span> <span class="o">=</span> <span class="n">dict_cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span><span class="p">[</span><span class="s">&#39;id&#39;</span><span class="p">]</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span><span class="p">[</span><span class="s">&#39;num&#39;</span><span class="p">]</span>
<span class="go">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span><span class="p">[</span><span class="s">&#39;data&#39;</span><span class="p">]</span>
<span class="go">&quot;abc&#39;def&quot;</span>
</pre></div>
</div>
<p>The records still support indexing as the original tuple:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="go">&quot;abc&#39;def&quot;</span>
</pre></div>
</div>
<dl class="class">
<dt id="psycopg2.extras.DictCursor">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">DictCursor</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#psycopg2.extras.DictCursor" title="Permalink to this definition">¶</a></dt>
<dd><p>A cursor that keeps a list of column name -&gt; index mappings.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.DictConnection">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">DictConnection</tt><a class="headerlink" href="#psycopg2.extras.DictConnection" title="Permalink to this definition">¶</a></dt>
<dd><p>A connection that uses <a class="reference internal" href="#psycopg2.extras.DictCursor" title="psycopg2.extras.DictCursor"><tt class="xref py py-obj docutils literal"><span class="pre">DictCursor</span></tt></a> automatically.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Not very useful since Psycopg 2.5: you can use <a class="reference internal" href="module.html#psycopg2.connect" title="psycopg2.connect"><tt class="xref py py-obj docutils literal"><span class="pre">psycopg2.connect</span></tt></a><tt class="docutils literal"><span class="pre">(dsn,</span> <span class="pre">cursor_factory=DictCursor)</span></tt> instead of <tt class="xref py py-obj docutils literal"><span class="pre">DictConnection</span></tt>.</p>
</div>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.DictRow">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">DictRow</tt><big>(</big><em>cursor</em><big>)</big><a class="headerlink" href="#psycopg2.extras.DictRow" title="Permalink to this definition">¶</a></dt>
<dd><p>A row object that allow by-column-name access to data.</p>
</dd></dl>

</div>
<div class="section" id="real-dictionary-cursor">
<h3>Real dictionary cursor<a class="headerlink" href="#real-dictionary-cursor" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="psycopg2.extras.RealDictCursor">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">RealDictCursor</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#psycopg2.extras.RealDictCursor" title="Permalink to this definition">¶</a></dt>
<dd><p>A cursor that uses a real dict as the base type for rows.</p>
<p>Note that this cursor is extremely specialized and does not allow
the normal access (using integer indices) to fetched data. If you need
to access database rows both as a dictionary and a list, then use
the generic <a class="reference internal" href="#psycopg2.extras.DictCursor" title="psycopg2.extras.DictCursor"><tt class="xref py py-obj docutils literal"><span class="pre">DictCursor</span></tt></a> instead of <tt class="xref py py-obj docutils literal"><span class="pre">RealDictCursor</span></tt>.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.RealDictConnection">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">RealDictConnection</tt><a class="headerlink" href="#psycopg2.extras.RealDictConnection" title="Permalink to this definition">¶</a></dt>
<dd><p>A connection that uses <a class="reference internal" href="#psycopg2.extras.RealDictCursor" title="psycopg2.extras.RealDictCursor"><tt class="xref py py-obj docutils literal"><span class="pre">RealDictCursor</span></tt></a> automatically.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Not very useful since Psycopg 2.5: you can use <a class="reference internal" href="module.html#psycopg2.connect" title="psycopg2.connect"><tt class="xref py py-obj docutils literal"><span class="pre">psycopg2.connect</span></tt></a><tt class="docutils literal"><span class="pre">(dsn,</span> <span class="pre">cursor_factory=RealDictCursor)</span></tt> instead of
<tt class="xref py py-obj docutils literal"><span class="pre">RealDictConnection</span></tt>.</p>
</div>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.RealDictRow">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">RealDictRow</tt><big>(</big><em>cursor</em><big>)</big><a class="headerlink" href="#psycopg2.extras.RealDictRow" title="Permalink to this definition">¶</a></dt>
<dd><p>A <tt class="xref py py-obj docutils literal"><span class="pre">dict</span></tt> subclass representing a data record.</p>
</dd></dl>

</div>
<div class="section" id="namedtuple-cursor">
<span id="index-1"></span><h3><tt class="xref py py-obj docutils literal"><span class="pre">namedtuple</span></tt> cursor<a class="headerlink" href="#namedtuple-cursor" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
<p>These objects require <a class="reference external" href="http://docs.python.org/3.2/library/collections.html#collections.namedtuple" title="(in Python v3.2)"><tt class="xref py py-func docutils literal"><span class="pre">collections.namedtuple()</span></tt></a> to be found, so it is
available out-of-the-box only from Python 2.6. Anyway, the namedtuple
implementation is compatible with previous Python versions, so all you
have to do is to <a class="reference external" href="http://code.activestate.com/recipes/500261-named-tuples/">download it</a> and make it available where we
expect it to be...</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">somewhere</span> <span class="kn">import</span> <span class="n">namedtuple</span>
<span class="kn">import</span> <span class="nn">collections</span>
<span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span> <span class="o">=</span> <span class="n">namedtuple</span>
<span class="kn">from</span> <span class="nn">psycopg.extras</span> <span class="kn">import</span> <span class="n">NamedTupleConnection</span>
<span class="c"># ...</span>
</pre></div>
</div>
<dl class="class">
<dt id="psycopg2.extras.NamedTupleCursor">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">NamedTupleCursor</tt><a class="headerlink" href="#psycopg2.extras.NamedTupleCursor" title="Permalink to this definition">¶</a></dt>
<dd><p>A cursor that generates results as <a class="reference external" href="http://docs.python.org/3.2/library/collections.html#collections.namedtuple" title="(in Python v3.2)"><tt class="xref py py-obj docutils literal"><span class="pre">namedtuple</span></tt></a>.</p>
<p><tt class="xref py py-obj docutils literal"><span class="pre">fetch*()</span></tt> methods will return named tuples instead of regular tuples, so
their elements can be accessed both as regular numeric items as well as
attributes.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">nt_cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">(</span><span class="n">cursor_factory</span><span class="o">=</span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">NamedTupleCursor</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span> <span class="o">=</span> <span class="n">nt_cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span>
<span class="go">Record(id=1, num=100, data=&quot;abc&#39;def&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rec</span><span class="o">.</span><span class="n">data</span>
<span class="go">&quot;abc&#39;def&quot;</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.NamedTupleConnection">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">NamedTupleConnection</tt><a class="headerlink" href="#psycopg2.extras.NamedTupleConnection" title="Permalink to this definition">¶</a></dt>
<dd><p>A connection that uses <a class="reference internal" href="#psycopg2.extras.NamedTupleCursor" title="psycopg2.extras.NamedTupleCursor"><tt class="xref py py-obj docutils literal"><span class="pre">NamedTupleCursor</span></tt></a> automatically.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Not very useful since Psycopg 2.5: you can use <a class="reference internal" href="module.html#psycopg2.connect" title="psycopg2.connect"><tt class="xref py py-obj docutils literal"><span class="pre">psycopg2.connect</span></tt></a><tt class="docutils literal"><span class="pre">(dsn,</span> <span class="pre">cursor_factory=NamedTupleCursor)</span></tt> instead of
<tt class="xref py py-obj docutils literal"><span class="pre">NamedTupleConnection</span></tt>.</p>
</div>
</dd></dl>

</div>
<div class="section" id="logging-cursor">
<span id="index-2"></span><h3>Logging cursor<a class="headerlink" href="#logging-cursor" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="psycopg2.extras.LoggingConnection">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">LoggingConnection</tt><a class="headerlink" href="#psycopg2.extras.LoggingConnection" title="Permalink to this definition">¶</a></dt>
<dd><p>A connection that logs all queries to a file or <a class="reference external" href="http://docs.python.org/library/logging.html">logger</a> object.</p>
<dl class="method">
<dt id="psycopg2.extras.LoggingConnection.filter">
<tt class="descname">filter</tt><big>(</big><em>msg</em>, <em>curs</em><big>)</big><a class="headerlink" href="#psycopg2.extras.LoggingConnection.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Filter the query before logging it.</p>
<p>This is the method to overwrite to filter unwanted queries out of the
log or to add some extra data to the output. The default implementation
just does nothing.</p>
</dd></dl>

<dl class="method">
<dt id="psycopg2.extras.LoggingConnection.initialize">
<tt class="descname">initialize</tt><big>(</big><em>logobj</em><big>)</big><a class="headerlink" href="#psycopg2.extras.LoggingConnection.initialize" title="Permalink to this definition">¶</a></dt>
<dd><p>Initialize the connection to log to <tt class="xref py py-obj docutils literal"><span class="pre">logobj</span></tt>.</p>
<p>The <tt class="xref py py-obj docutils literal"><span class="pre">logobj</span></tt> parameter can be an open file object or a Logger
instance from the standard logging module.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.LoggingCursor">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">LoggingCursor</tt><a class="headerlink" href="#psycopg2.extras.LoggingCursor" title="Permalink to this definition">¶</a></dt>
<dd><p>A cursor that logs queries using its connection logging facilities.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.MinTimeLoggingConnection">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">MinTimeLoggingConnection</tt><a class="headerlink" href="#psycopg2.extras.MinTimeLoggingConnection" title="Permalink to this definition">¶</a></dt>
<dd><p>A connection that logs queries based on execution time.</p>
<p>This is just an example of how to sub-class <a class="reference internal" href="#psycopg2.extras.LoggingConnection" title="psycopg2.extras.LoggingConnection"><tt class="xref py py-obj docutils literal"><span class="pre">LoggingConnection</span></tt></a> to
provide some extra filtering for the logged queries. Both the
<tt class="xref py py-obj docutils literal"><span class="pre">initialize()</span></tt> and <tt class="xref py py-obj docutils literal"><span class="pre">filter()</span></tt> methods are overwritten to make sure
that only queries executing for more than <tt class="docutils literal"><span class="pre">mintime</span></tt> ms are logged.</p>
<p>Note that this connection uses the specialized cursor
<a class="reference internal" href="#psycopg2.extras.MinTimeLoggingCursor" title="psycopg2.extras.MinTimeLoggingCursor"><tt class="xref py py-obj docutils literal"><span class="pre">MinTimeLoggingCursor</span></tt></a>.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.MinTimeLoggingCursor">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">MinTimeLoggingCursor</tt><a class="headerlink" href="#psycopg2.extras.MinTimeLoggingCursor" title="Permalink to this definition">¶</a></dt>
<dd><p>The cursor sub-class companion to <a class="reference internal" href="#psycopg2.extras.MinTimeLoggingConnection" title="psycopg2.extras.MinTimeLoggingConnection"><tt class="xref py py-obj docutils literal"><span class="pre">MinTimeLoggingConnection</span></tt></a>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="additional-data-types">
<span id="index-3"></span><h2>Additional data types<a class="headerlink" href="#additional-data-types" title="Permalink to this headline">¶</a></h2>
<div class="section" id="json-adaptation">
<span id="adapt-json"></span><span id="index-4"></span><h3><a class="reference external" href="http://www.json.org/">JSON</a> adaptation<a class="headerlink" href="#json-adaptation" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.5.4: </span>added <tt class="sql docutils literal"><span class="pre">jsonb</span></tt> support. In previous versions <tt class="sql docutils literal"><span class="pre">jsonb</span></tt> values are returned
as strings. See <a class="reference internal" href="faq.html#faq-jsonb-adapt"><em>the FAQ</em></a> for a workaround.</p>
</div>
<p>Psycopg can adapt Python objects to and from the PostgreSQL <a class="reference external" href="http://www.postgresql.org/docs/current/static/datatype-json.html"><tt class="sql docutils literal"><span class="pre">json</span></tt></a> and
<tt class="sql docutils literal"><span class="pre">jsonb</span></tt> types.  With PostgreSQL 9.2 and following versions adaptation is
available out-of-the-box. To use JSON data with previous database versions
(either with the <a class="reference external" href="http://people.planetpostgresql.org/andrew/index.php?/archives/255-JSON-for-PG-9.2-...-and-now-for-9.1!.html">9.1 json extension</a>, but even if you want to convert text
fields to JSON) you can use the <a class="reference internal" href="#psycopg2.extras.register_json" title="psycopg2.extras.register_json"><tt class="xref py py-obj docutils literal"><span class="pre">register_json()</span></tt></a> function.</p>
<p>The Python library used by default to convert Python objects to JSON and to
parse data from the database depends on the language version: with Python 2.6
and following the <a class="reference external" href="http://docs.python.org/3.2/library/json.html#module-json" title="(in Python v3.2)"><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> module from the standard library is used;
with previous versions the <a class="reference external" href="http://pypi.python.org/pypi/simplejson/">simplejson</a> module is used if available. Note
that the last <tt class="xref py py-obj docutils literal"><span class="pre">simplejson</span></tt> version supporting Python 2.4 is the 2.0.9.</p>
<p>In order to pass a Python object to the database as query argument you can use
the <a class="reference internal" href="#psycopg2.extras.Json" title="psycopg2.extras.Json"><tt class="xref py py-obj docutils literal"><span class="pre">Json</span></tt></a> adapter:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">curs</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into mytable (jsondata) values (</span><span class="si">%s</span><span class="s">)&quot;</span><span class="p">,</span>
    <span class="p">[</span><span class="n">Json</span><span class="p">({</span><span class="s">&#39;a&#39;</span><span class="p">:</span> <span class="mi">100</span><span class="p">})])</span>
</pre></div>
</div>
<p>Reading from the database, <tt class="sql docutils literal"><span class="pre">json</span></tt> and <tt class="sql docutils literal"><span class="pre">jsonb</span></tt> values will be automatically
converted to Python objects.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>If you are using the PostgreSQL <tt class="sql docutils literal"><span class="pre">json</span></tt> data type but you want to read
it as string in Python instead of having it parsed, your can either cast
the column to <tt class="sql docutils literal"><span class="pre">text</span></tt> in the query (it is an efficient operation, that
doesn&#8217;t involve a copy):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select jsondata::text from mytable&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>or you can register a no-op <tt class="xref py py-obj docutils literal"><span class="pre">loads()</span></tt> function with
<a class="reference internal" href="#psycopg2.extras.register_default_json" title="psycopg2.extras.register_default_json"><tt class="xref py py-obj docutils literal"><span class="pre">register_default_json()</span></tt></a>:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">register_default_json</span><span class="p">(</span><span class="n">loads</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>You can use <a class="reference internal" href="extensions.html#psycopg2.extensions.register_adapter" title="psycopg2.extensions.register_adapter"><tt class="xref py py-obj docutils literal"><span class="pre">register_adapter()</span></tt></a> to adapt any Python
dictionary to JSON, either registering <a class="reference internal" href="#psycopg2.extras.Json" title="psycopg2.extras.Json"><tt class="xref py py-obj docutils literal"><span class="pre">Json</span></tt></a> or any subclass or factory
creating a compatible adapter:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">psycopg2</span><span class="o">.</span><span class="n">extensions</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="nb">dict</span><span class="p">,</span> <span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">Json</span><span class="p">)</span>
</pre></div>
</div>
<p class="last">This setting is global though, so it is not compatible with similar
adapters such as the one registered by <a class="reference internal" href="#psycopg2.extras.register_hstore" title="psycopg2.extras.register_hstore"><tt class="xref py py-obj docutils literal"><span class="pre">register_hstore()</span></tt></a>. Any other
object supported by JSON can be registered the same way, but this will
clobber the default adaptation rule, so be careful to unwanted side
effects.</p>
</div>
<p>If you want to customize the adaptation from Python to PostgreSQL you can
either provide a custom <tt class="xref py py-obj docutils literal"><span class="pre">dumps()</span></tt> function to <a class="reference internal" href="#psycopg2.extras.Json" title="psycopg2.extras.Json"><tt class="xref py py-obj docutils literal"><span class="pre">Json</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">curs</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into mytable (jsondata) values (</span><span class="si">%s</span><span class="s">)&quot;</span><span class="p">,</span>
    <span class="p">[</span><span class="n">Json</span><span class="p">({</span><span class="s">&#39;a&#39;</span><span class="p">:</span> <span class="mi">100</span><span class="p">},</span> <span class="n">dumps</span><span class="o">=</span><span class="n">simplejson</span><span class="o">.</span><span class="n">dumps</span><span class="p">)])</span>
</pre></div>
</div>
<p>or you can subclass it overriding the <a class="reference internal" href="#psycopg2.extras.Json.dumps" title="psycopg2.extras.Json.dumps"><tt class="xref py py-obj docutils literal"><span class="pre">dumps()</span></tt></a> method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyJson</span><span class="p">(</span><span class="n">Json</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">dumps</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">simplejson</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

<span class="n">curs</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into mytable (jsondata) values (</span><span class="si">%s</span><span class="s">)&quot;</span><span class="p">,</span>
    <span class="p">[</span><span class="n">MyJson</span><span class="p">({</span><span class="s">&#39;a&#39;</span><span class="p">:</span> <span class="mi">100</span><span class="p">})])</span>
</pre></div>
</div>
<p>Customizing the conversion from PostgreSQL to Python can be done passing a
custom <tt class="xref py py-obj docutils literal"><span class="pre">loads()</span></tt> function to <a class="reference internal" href="#psycopg2.extras.register_json" title="psycopg2.extras.register_json"><tt class="xref py py-obj docutils literal"><span class="pre">register_json()</span></tt></a>.  For the builtin data types
(<tt class="sql docutils literal"><span class="pre">json</span></tt> from PostgreSQL 9.2, <tt class="sql docutils literal"><span class="pre">jsonb</span></tt> from PostgreSQL 9.4) use
<a class="reference internal" href="#psycopg2.extras.register_default_json" title="psycopg2.extras.register_default_json"><tt class="xref py py-obj docutils literal"><span class="pre">register_default_json()</span></tt></a> and <a class="reference internal" href="#psycopg2.extras.register_default_jsonb" title="psycopg2.extras.register_default_jsonb"><tt class="xref py py-obj docutils literal"><span class="pre">register_default_jsonb()</span></tt></a>.  For example, if you
want to convert the float values from <tt class="sql docutils literal"><span class="pre">json</span></tt> into
<a class="reference external" href="http://docs.python.org/3.2/library/decimal.html#decimal.Decimal" title="(in Python v3.2)"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> you can use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">loads</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">parse_float</span><span class="o">=</span><span class="n">Decimal</span><span class="p">)</span>
<span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">register_json</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">loads</span><span class="o">=</span><span class="n">loads</span><span class="p">)</span>
</pre></div>
</div>
<dl class="class">
<dt id="psycopg2.extras.Json">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">Json</tt><big>(</big><em>adapted</em>, <em>dumps=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.Json" title="Permalink to this definition">¶</a></dt>
<dd><p>An <a class="reference internal" href="extensions.html#psycopg2.extensions.ISQLQuote" title="psycopg2.extensions.ISQLQuote"><tt class="xref py py-obj docutils literal"><span class="pre">ISQLQuote</span></tt></a> wrapper to adapt a Python object to
<tt class="sql docutils literal"><span class="pre">json</span></tt> data type.</p>
<p><tt class="xref py py-obj docutils literal"><span class="pre">Json</span></tt> can be used to wrap any object supported by the provided <em>dumps</em>
function.  If none is provided, the standard <a class="reference external" href="http://docs.python.org/3.2/library/json.html#json.dumps" title="(in Python v3.2)"><tt class="xref py py-func docutils literal"><span class="pre">json.dumps()</span></tt></a> is
used (<tt class="xref py py-obj docutils literal"><span class="pre">simplejson</span></tt> for Python &lt; 2.6;
<a class="reference internal" href="extensions.html#psycopg2.extensions.ISQLQuote.getquoted" title="psycopg2.extensions.ISQLQuote.getquoted"><tt class="xref py py-obj docutils literal"><span class="pre">getquoted()</span></tt></a> will raise <tt class="xref py py-obj docutils literal"><span class="pre">ImportError</span></tt> if
the module is not available).</p>
<dl class="method">
<dt id="psycopg2.extras.Json.dumps">
<tt class="descname">dumps</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#psycopg2.extras.Json.dumps" title="Permalink to this definition">¶</a></dt>
<dd><p>Serialize <em>obj</em> in JSON format.</p>
<p>The default is to call <tt class="xref py py-obj docutils literal"><span class="pre">json.dumps()</span></tt> or the <em>dumps</em> function
provided in the constructor. You can override this method to create a
customized JSON wrapper.</p>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="psycopg2.extras.register_json">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_json</tt><big>(</big><em>conn_or_curs=None</em>, <em>globally=False</em>, <em>loads=None</em>, <em>oid=None</em>, <em>array_oid=None</em>, <em>name='json'</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_json" title="Permalink to this definition">¶</a></dt>
<dd><p>Create and register typecasters converting <tt class="sql docutils literal"><span class="pre">json</span></tt> type to Python objects.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>conn_or_curs</strong> &#8211; a connection or cursor used to find the <tt class="sql docutils literal"><span class="pre">json</span></tt>
and <tt class="sql docutils literal"><span class="pre">json[]</span></tt> oids; the typecasters are registered in a scope
limited to this object, unless <em>globally</em> is set to <tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt>. It can be
<tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt> if the oids are provided</li>
<li><strong>globally</strong> &#8211; if <tt class="xref py py-obj docutils literal"><span class="pre">False</span></tt> register the typecasters only on
<em>conn_or_curs</em>, otherwise register them globally</li>
<li><strong>loads</strong> &#8211; the function used to parse the data into a Python object. If
<tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt> use <tt class="xref py py-obj docutils literal"><span class="pre">json.loads()</span></tt>, where <tt class="xref py py-obj docutils literal"><span class="pre">json</span></tt> is the module chosen
according to the Python version (see above)</li>
<li><strong>oid</strong> &#8211; the OID of the <tt class="sql docutils literal"><span class="pre">json</span></tt> type if known; If not, it will be
queried on <em>conn_or_curs</em></li>
<li><strong>array_oid</strong> &#8211; the OID of the <tt class="sql docutils literal"><span class="pre">json[]</span></tt> array type if known;
if not, it will be queried on <em>conn_or_curs</em></li>
<li><strong>name</strong> &#8211; the name of the data type to look for in <em>conn_or_curs</em></li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>The connection or cursor passed to the function will be used to query the
database and look for the OID of the <tt class="sql docutils literal"><span class="pre">json</span></tt> type (or an alternative
type if <em>name</em> if provided). No query is performed if <em>oid</em> and <em>array_oid</em>
are provided.  Raise <a class="reference internal" href="module.html#psycopg2.ProgrammingError" title="psycopg2.ProgrammingError"><tt class="xref py py-obj docutils literal"><span class="pre">ProgrammingError</span></tt></a> if the type is not found.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.5.4: </span>added the <em>name</em> parameter to enable <tt class="sql docutils literal"><span class="pre">jsonb</span></tt> support.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="psycopg2.extras.register_default_json">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_default_json</tt><big>(</big><em>conn_or_curs=None</em>, <em>globally=False</em>, <em>loads=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_default_json" title="Permalink to this definition">¶</a></dt>
<dd><p>Create and register <tt class="sql docutils literal"><span class="pre">json</span></tt> typecasters for PostgreSQL 9.2 and following.</p>
<p>Since PostgreSQL 9.2 <tt class="sql docutils literal"><span class="pre">json</span></tt> is a builtin type, hence its oid is known
and fixed. This function allows specifying a customized <em>loads</em> function
for the default <tt class="sql docutils literal"><span class="pre">json</span></tt> type without querying the database.
All the parameters have the same meaning of <a class="reference internal" href="#psycopg2.extras.register_json" title="psycopg2.extras.register_json"><tt class="xref py py-obj docutils literal"><span class="pre">register_json()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="psycopg2.extras.register_default_jsonb">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_default_jsonb</tt><big>(</big><em>conn_or_curs=None</em>, <em>globally=False</em>, <em>loads=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_default_jsonb" title="Permalink to this definition">¶</a></dt>
<dd><p>Create and register <tt class="sql docutils literal"><span class="pre">jsonb</span></tt> typecasters for PostgreSQL 9.4 and following.</p>
<p>As in <a class="reference internal" href="#psycopg2.extras.register_default_json" title="psycopg2.extras.register_default_json"><tt class="xref py py-obj docutils literal"><span class="pre">register_default_json()</span></tt></a>, the function allows to register a
customized <em>loads</em> function for the <tt class="sql docutils literal"><span class="pre">jsonb</span></tt> type at its known oid for
PostgreSQL 9.4 and following versions.  All the parameters have the same
meaning of <a class="reference internal" href="#psycopg2.extras.register_json" title="psycopg2.extras.register_json"><tt class="xref py py-obj docutils literal"><span class="pre">register_json()</span></tt></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.4.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="hstore-data-type">
<span id="adapt-hstore"></span><span id="index-5"></span><h3>Hstore data type<a class="headerlink" href="#hstore-data-type" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
<p>The <a class="reference external" href="http://www.postgresql.org/docs/current/static/hstore.html"><tt class="sql docutils literal"><span class="pre">hstore</span></tt></a> data type is a key-value store embedded in PostgreSQL.  It has
been available for several server versions but with the release 9.0 it has
been greatly improved in capacity and usefulness with the addition of many
functions.  It supports GiST or GIN indexes allowing search by keys or
key/value pairs as well as regular BTree indexes for equality, uniqueness etc.</p>
<p>Psycopg can convert Python <tt class="xref py py-obj docutils literal"><span class="pre">dict</span></tt> objects to and from <tt class="sql docutils literal"><span class="pre">hstore</span></tt> structures.
Only dictionaries with string/unicode keys and values are supported.  <tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt>
is also allowed as value but not as a key. Psycopg uses a more efficient <tt class="sql docutils literal"><span class="pre">hstore</span></tt>
representation when dealing with PostgreSQL 9.0 but previous server versions
are supported as well.  By default the adapter/typecaster are disabled: they
can be enabled using the <a class="reference internal" href="#psycopg2.extras.register_hstore" title="psycopg2.extras.register_hstore"><tt class="xref py py-obj docutils literal"><span class="pre">register_hstore()</span></tt></a> function.</p>
<dl class="function">
<dt id="psycopg2.extras.register_hstore">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_hstore</tt><big>(</big><em>conn_or_curs</em>, <em>globally=False</em>, <em>unicode=False</em>, <em>oid=None</em>, <em>array_oid=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_hstore" title="Permalink to this definition">¶</a></dt>
<dd><p>Register adapter and typecaster for <tt class="xref py py-obj docutils literal"><span class="pre">dict</span></tt>-<tt class="sql docutils literal"><span class="pre">hstore</span></tt> conversions.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>conn_or_curs</strong> &#8211; a connection or cursor: the typecaster will be
registered only on this object unless <em>globally</em> is set to <tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt></li>
<li><strong>globally</strong> &#8211; register the adapter globally, not only on <em>conn_or_curs</em></li>
<li><strong>unicode</strong> &#8211; if <tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt>, keys and values returned from the database
will be <tt class="xref py py-obj docutils literal"><span class="pre">unicode</span></tt> instead of <tt class="xref py py-obj docutils literal"><span class="pre">str</span></tt>. The option is not available on
Python 3</li>
<li><strong>oid</strong> &#8211; the OID of the <tt class="sql docutils literal"><span class="pre">hstore</span></tt> type if known. If not, it will be
queried on <em>conn_or_curs</em>.</li>
<li><strong>array_oid</strong> &#8211; the OID of the <tt class="sql docutils literal"><span class="pre">hstore</span></tt> array type if known. If not, it
will be queried on <em>conn_or_curs</em>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>The connection or cursor passed to the function will be used to query the
database and look for the OID of the <tt class="sql docutils literal"><span class="pre">hstore</span></tt> type (which may be different
across databases). If querying is not desirable (e.g. with
<a class="reference internal" href="advanced.html#async-support"><em>asynchronous connections</em></a>) you may specify it in the
<em>oid</em> parameter, which can be found using a query such as <tt class="sql docutils literal"><span class="pre">SELECT</span>
<span class="pre">'hstore'::regtype::oid</span></tt>. Analogously you can obtain a value for <em>array_oid</em>
using a query such as <tt class="sql docutils literal"><span class="pre">SELECT</span> <span class="pre">'hstore[]'::regtype::oid</span></tt>.</p>
<p>Note that, when passing a dictionary from Python to the database, both
strings and unicode keys and values are supported. Dictionaries returned
from the database have keys/values according to the <em>unicode</em> parameter.</p>
<p>The <tt class="sql docutils literal"><span class="pre">hstore</span></tt> contrib module must be already installed in the database
(executing the <tt class="docutils literal"><span class="pre">hstore.sql</span></tt> script in your <tt class="docutils literal"><span class="pre">contrib</span></tt> directory).
Raise <a class="reference internal" href="module.html#psycopg2.ProgrammingError" title="psycopg2.ProgrammingError"><tt class="xref py py-obj docutils literal"><span class="pre">ProgrammingError</span></tt></a> if the type is not found.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>added the <em>oid</em> parameter. If not specified, the typecaster is
installed also if <tt class="sql docutils literal"><span class="pre">hstore</span></tt> is not installed in the <tt class="sql docutils literal"><span class="pre">public</span></tt>
schema.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4.3: </span>added support for <tt class="sql docutils literal"><span class="pre">hstore</span></tt> array.</p>
</div>
</dd></dl>

</div>
<div class="section" id="composite-types-casting">
<span id="adapt-composite"></span><span id="index-6"></span><h3>Composite types casting<a class="headerlink" href="#composite-types-casting" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
<p>Using <a class="reference internal" href="#psycopg2.extras.register_composite" title="psycopg2.extras.register_composite"><tt class="xref py py-obj docutils literal"><span class="pre">register_composite()</span></tt></a> it is possible to cast a PostgreSQL composite
type (either created with the <a class="reference external" href="http://www.postgresql.org/docs/current/static/sql-createtype.html"><tt class="sql docutils literal"><span class="pre">CREATE</span> <span class="pre">TYPE</span></tt></a> command or implicitly defined
after a table row type) into a Python named tuple, or into a regular tuple if
<a class="reference external" href="http://docs.python.org/3.2/library/collections.html#collections.namedtuple" title="(in Python v3.2)"><tt class="xref py py-func docutils literal"><span class="pre">collections.namedtuple()</span></tt></a> is not found.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;CREATE TYPE card AS (value int, suit text);&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">register_composite</span><span class="p">(</span><span class="s">&#39;card&#39;</span><span class="p">,</span> <span class="n">cur</span><span class="p">)</span>
<span class="go">&lt;psycopg2.extras.CompositeCaster object at 0x...&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select (8, &#39;hearts&#39;)::card&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">card(value=8, suit=&#39;hearts&#39;)</span>
</pre></div>
</div>
<p>Nested composite types are handled as expected, provided that the type of the
composite components are registered as well.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;CREATE TYPE card_back AS (face card, back text);&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">register_composite</span><span class="p">(</span><span class="s">&#39;card_back&#39;</span><span class="p">,</span> <span class="n">cur</span><span class="p">)</span>
<span class="go">&lt;psycopg2.extras.CompositeCaster object at 0x...&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ((8, &#39;hearts&#39;), &#39;blue&#39;)::card_back&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">card_back(face=card(value=8, suit=&#39;hearts&#39;), back=&#39;blue&#39;)</span>
</pre></div>
</div>
<p>Adaptation from Python tuples to composite types is automatic instead and
requires no adapter registration.</p>
<div class="admonition note" id="custom-composite">
<p class="first admonition-title">Note</p>
<p>If you want to convert PostgreSQL composite types into something different
than a <tt class="xref py py-obj docutils literal"><span class="pre">namedtuple</span></tt> you can subclass the <a class="reference internal" href="#psycopg2.extras.CompositeCaster" title="psycopg2.extras.CompositeCaster"><tt class="xref py py-obj docutils literal"><span class="pre">CompositeCaster</span></tt></a> overriding
<a class="reference internal" href="#psycopg2.extras.CompositeCaster.make" title="psycopg2.extras.CompositeCaster.make"><tt class="xref py py-obj docutils literal"><span class="pre">make()</span></tt></a>. For example, if you want to convert your type
into a Python dictionary you can use:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">DictComposite</span><span class="p">(</span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">CompositeCaster</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">make</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">attnames</span><span class="p">,</span> <span class="n">values</span><span class="p">))</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">register_composite</span><span class="p">(</span><span class="s">&#39;card&#39;</span><span class="p">,</span> <span class="n">cur</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">factory</span><span class="o">=</span><span class="n">DictComposite</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select (8, &#39;hearts&#39;)::card&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">{&#39;suit&#39;: &#39;hearts&#39;, &#39;value&#39;: 8}</span>
</pre></div>
</div>
</div>
<dl class="function">
<dt id="psycopg2.extras.register_composite">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_composite</tt><big>(</big><em>name</em>, <em>conn_or_curs</em>, <em>globally=False</em>, <em>factory=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_composite" title="Permalink to this definition">¶</a></dt>
<dd><p>Register a typecaster to convert a composite type into a tuple.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>name</strong> &#8211; the name of a PostgreSQL composite type, e.g. created using
the <a class="reference external" href="http://www.postgresql.org/docs/current/static/sql-createtype.html"><tt class="sql docutils literal"><span class="pre">CREATE</span> <span class="pre">TYPE</span></tt></a> command</li>
<li><strong>conn_or_curs</strong> &#8211; a connection or cursor used to find the type oid and
components; the typecaster is registered in a scope limited to this
object, unless <em>globally</em> is set to <tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt></li>
<li><strong>globally</strong> &#8211; if <tt class="xref py py-obj docutils literal"><span class="pre">False</span></tt> (default) register the typecaster only on
<em>conn_or_curs</em>, otherwise register it globally</li>
<li><strong>factory</strong> &#8211; if specified it should be a <a class="reference internal" href="#psycopg2.extras.CompositeCaster" title="psycopg2.extras.CompositeCaster"><tt class="xref py py-obj docutils literal"><span class="pre">CompositeCaster</span></tt></a> subclass: use
it to <a class="reference internal" href="#custom-composite"><em>customize how to cast composite types</em></a></li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">the registered <a class="reference internal" href="#psycopg2.extras.CompositeCaster" title="psycopg2.extras.CompositeCaster"><tt class="xref py py-obj docutils literal"><span class="pre">CompositeCaster</span></tt></a> or <em>factory</em> instance
responsible for the conversion</p>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4.3: </span>added support for array of composite types</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.5: </span>added the <em>factory</em> parameter</p>
</div>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.CompositeCaster">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">CompositeCaster</tt><big>(</big><em>name</em>, <em>oid</em>, <em>attrs</em>, <em>array_oid=None</em>, <em>schema=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.CompositeCaster" title="Permalink to this definition">¶</a></dt>
<dd><p>Helps conversion of a PostgreSQL composite type into a Python object.</p>
<p>The class is usually created by the <a class="reference internal" href="#psycopg2.extras.register_composite" title="psycopg2.extras.register_composite"><tt class="xref py py-obj docutils literal"><span class="pre">register_composite()</span></tt></a> function.
You may want to create and register manually instances of the class if
querying the database at registration time is not desirable (such as when
using an <a class="reference internal" href="advanced.html#async-support"><em>asynchronous connections</em></a>).</p>
<dl class="method">
<dt id="psycopg2.extras.CompositeCaster.make">
<tt class="descname">make</tt><big>(</big><em>values</em><big>)</big><a class="headerlink" href="#psycopg2.extras.CompositeCaster.make" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new Python object representing the data being casted.</p>
<p><em>values</em> is the list of attributes, already casted into their Python
representation.</p>
<p>You can subclass this method to <a class="reference internal" href="#custom-composite"><em>customize the composite cast</em></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd></dl>

<p>Object attributes:</p>
<dl class="attribute">
<dt id="psycopg2.extras.CompositeCaster.name">
<tt class="descname">name</tt><a class="headerlink" href="#psycopg2.extras.CompositeCaster.name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of the PostgreSQL type.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.CompositeCaster.schema">
<tt class="descname">schema</tt><a class="headerlink" href="#psycopg2.extras.CompositeCaster.schema" title="Permalink to this definition">¶</a></dt>
<dd><p>The schema where the type is defined.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.CompositeCaster.oid">
<tt class="descname">oid</tt><a class="headerlink" href="#psycopg2.extras.CompositeCaster.oid" title="Permalink to this definition">¶</a></dt>
<dd><p>The oid of the PostgreSQL type.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.CompositeCaster.array_oid">
<tt class="descname">array_oid</tt><a class="headerlink" href="#psycopg2.extras.CompositeCaster.array_oid" title="Permalink to this definition">¶</a></dt>
<dd><p>The oid of the PostgreSQL array type, if available.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.CompositeCaster.type">
<tt class="descname">type</tt><a class="headerlink" href="#psycopg2.extras.CompositeCaster.type" title="Permalink to this definition">¶</a></dt>
<dd><p>The type of the Python objects returned. If <a class="reference external" href="http://docs.python.org/3.2/library/collections.html#collections.namedtuple" title="(in Python v3.2)"><tt class="xref py py-func docutils literal"><span class="pre">collections.namedtuple()</span></tt></a>
is available, it is a named tuple with attributes equal to the type
components. Otherwise it is just the <tt class="xref py py-obj docutils literal"><span class="pre">tuple</span></tt> object.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.CompositeCaster.attnames">
<tt class="descname">attnames</tt><a class="headerlink" href="#psycopg2.extras.CompositeCaster.attnames" title="Permalink to this definition">¶</a></dt>
<dd><p>List of component names of the type to be casted.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.CompositeCaster.atttypes">
<tt class="descname">atttypes</tt><a class="headerlink" href="#psycopg2.extras.CompositeCaster.atttypes" title="Permalink to this definition">¶</a></dt>
<dd><p>List of component type oids of the type to be casted.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="range-data-types">
<span id="adapt-range"></span><span id="index-7"></span><h3>Range data types<a class="headerlink" href="#range-data-types" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
<p>Psycopg offers a <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> Python type and supports adaptation between them and
PostgreSQL <a class="reference external" href="http://www.postgresql.org/docs/current/static/rangetypes.html"><tt class="sql docutils literal"><span class="pre">range</span></tt></a> types. Builtin <tt class="sql docutils literal"><span class="pre">range</span></tt> types are supported out-of-the-box;
user-defined <tt class="sql docutils literal"><span class="pre">range</span></tt> types can be adapted using <a class="reference internal" href="#psycopg2.extras.register_range" title="psycopg2.extras.register_range"><tt class="xref py py-obj docutils literal"><span class="pre">register_range()</span></tt></a>.</p>
<dl class="class">
<dt id="psycopg2.extras.Range">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">Range</tt><big>(</big><em>lower=None</em>, <em>upper=None</em>, <em>bounds='[)'</em>, <em>empty=False</em><big>)</big><a class="headerlink" href="#psycopg2.extras.Range" title="Permalink to this definition">¶</a></dt>
<dd><p>Python representation for a PostgreSQL <a class="reference external" href="http://www.postgresql.org/docs/current/static/rangetypes.html"><tt class="sql docutils literal"><span class="pre">range</span></tt></a> type.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>lower</strong> &#8211; lower bound for the range. <tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt> means unbound</li>
<li><strong>upper</strong> &#8211; upper bound for the range. <tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt> means unbound</li>
<li><strong>bounds</strong> &#8211; one of the literal strings <tt class="docutils literal"><span class="pre">()</span></tt>, <tt class="docutils literal"><span class="pre">[)</span></tt>, <tt class="docutils literal"><span class="pre">(]</span></tt>, <tt class="docutils literal"><span class="pre">[]</span></tt>,
representing whether the lower or upper bounds are included</li>
<li><strong>empty</strong> &#8211; if <tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt>, the range is empty</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>This Python type is only used to pass and retrieve range values to and
from PostgreSQL and doesn&#8217;t attempt to replicate the PostgreSQL range
features: it doesn&#8217;t perform normalization and doesn&#8217;t implement all the
<a class="reference external" href="http://www.postgresql.org/docs/current/static/functions-range.html#RANGE-OPERATORS-TABLE">operators</a> supported by the database.</p>
<p><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt> objects are immutable, hashable, and support the <tt class="docutils literal"><span class="pre">in</span></tt> operator
(checking if an element is within the range). They can be tested for
equivalence. Empty ranges evaluate to <tt class="xref py py-obj docutils literal"><span class="pre">False</span></tt> in boolean context,
nonempty evaluate to <tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.5.3: </span><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt> objects can be sorted although, as on the server-side, this
ordering is not particularly meangingful. It is only meant to be used
by programs assuming objects using <tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt> as primary key can be
sorted on them. In previous versions comparing <tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt>s raises
<tt class="xref py py-obj docutils literal"><span class="pre">TypeError</span></tt>.</p>
</div>
<p>Although it is possible to instantiate <tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt> objects, the class doesn&#8217;t
have an adapter registered, so you cannot normally pass these instances as
query arguments. To use range objects as query arguments you can either
use one of the provided subclasses, such as <a class="reference internal" href="#psycopg2.extras.NumericRange" title="psycopg2.extras.NumericRange"><tt class="xref py py-obj docutils literal"><span class="pre">NumericRange</span></tt></a> or create a
custom subclass using <a class="reference internal" href="#psycopg2.extras.register_range" title="psycopg2.extras.register_range"><tt class="xref py py-obj docutils literal"><span class="pre">register_range()</span></tt></a>.</p>
<p>Object attributes:</p>
<dl class="attribute">
<dt id="psycopg2.extras.Range.isempty">
<tt class="descname">isempty</tt><a class="headerlink" href="#psycopg2.extras.Range.isempty" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt> if the range is empty.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.Range.lower">
<tt class="descname">lower</tt><a class="headerlink" href="#psycopg2.extras.Range.lower" title="Permalink to this definition">¶</a></dt>
<dd><p>The lower bound of the range. <tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt> if empty or unbound.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.Range.upper">
<tt class="descname">upper</tt><a class="headerlink" href="#psycopg2.extras.Range.upper" title="Permalink to this definition">¶</a></dt>
<dd><p>The upper bound of the range. <tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt> if empty or unbound.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.Range.lower_inc">
<tt class="descname">lower_inc</tt><a class="headerlink" href="#psycopg2.extras.Range.lower_inc" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt> if the lower bound is included in the range.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.Range.upper_inc">
<tt class="descname">upper_inc</tt><a class="headerlink" href="#psycopg2.extras.Range.upper_inc" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt> if the upper bound is included in the range.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.Range.lower_inf">
<tt class="descname">lower_inf</tt><a class="headerlink" href="#psycopg2.extras.Range.lower_inf" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt> if the range doesn&#8217;t have a lower bound.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.Range.upper_inf">
<tt class="descname">upper_inf</tt><a class="headerlink" href="#psycopg2.extras.Range.upper_inf" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt> if the range doesn&#8217;t have an upper bound.</p>
</dd></dl>

</dd></dl>

<p>The following <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> subclasses map builtin PostgreSQL <tt class="sql docutils literal"><span class="pre">range</span></tt> types to
Python objects: they have an adapter registered so their instances can be
passed as query arguments. <tt class="sql docutils literal"><span class="pre">range</span></tt> values read from database queries are
automatically casted into instances of these classes.</p>
<dl class="class">
<dt id="psycopg2.extras.NumericRange">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">NumericRange</tt><big>(</big><em>lower=None</em>, <em>upper=None</em>, <em>bounds='[)'</em>, <em>empty=False</em><big>)</big><a class="headerlink" href="#psycopg2.extras.NumericRange" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> suitable to pass Python numeric types to a PostgreSQL range.</p>
<p>PostgreSQL types <tt class="sql docutils literal"><span class="pre">int4range</span></tt>, <tt class="sql docutils literal"><span class="pre">int8range</span></tt>, <tt class="sql docutils literal"><span class="pre">numrange</span></tt> are
casted into <tt class="xref py py-obj docutils literal"><span class="pre">NumericRange</span></tt> instances.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.DateRange">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">DateRange</tt><big>(</big><em>lower=None</em>, <em>upper=None</em>, <em>bounds='[)'</em>, <em>empty=False</em><big>)</big><a class="headerlink" href="#psycopg2.extras.DateRange" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents <tt class="sql docutils literal"><span class="pre">daterange</span></tt> values.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.DateTimeRange">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">DateTimeRange</tt><big>(</big><em>lower=None</em>, <em>upper=None</em>, <em>bounds='[)'</em>, <em>empty=False</em><big>)</big><a class="headerlink" href="#psycopg2.extras.DateTimeRange" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents <tt class="sql docutils literal"><span class="pre">tsrange</span></tt> values.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.DateTimeTZRange">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">DateTimeTZRange</tt><big>(</big><em>lower=None</em>, <em>upper=None</em>, <em>bounds='[)'</em>, <em>empty=False</em><big>)</big><a class="headerlink" href="#psycopg2.extras.DateTimeTZRange" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents <tt class="sql docutils literal"><span class="pre">tstzrange</span></tt> values.</p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Python lacks a representation for <tt class="sql docutils literal"><span class="pre">infinity</span></tt> date so Psycopg converts
the value to <tt class="xref py py-obj docutils literal"><span class="pre">date.max</span></tt> and such. When written into the database these
dates will assume their literal value (e.g. <tt class="sql docutils literal"><span class="pre">9999-12-31</span></tt> instead of
<tt class="sql docutils literal"><span class="pre">infinity</span></tt>).  Check <a class="reference internal" href="usage.html#infinite-dates-handling"><em>Infinite dates handling</em></a> for an example of
an alternative adapter to map <tt class="xref py py-obj docutils literal"><span class="pre">date.max</span></tt> to <tt class="sql docutils literal"><span class="pre">infinity</span></tt>. An
alternative dates adapter will be used automatically by the <a class="reference internal" href="#psycopg2.extras.DateRange" title="psycopg2.extras.DateRange"><tt class="xref py py-obj docutils literal"><span class="pre">DateRange</span></tt></a>
adapter and so on.</p>
</div>
<p>Custom <tt class="sql docutils literal"><span class="pre">range</span></tt> types (created with <a class="reference external" href="http://www.postgresql.org/docs/current/static/sql-createtype.html"><tt class="sql docutils literal"><span class="pre">CREATE</span> <span class="pre">TYPE</span></tt></a> <tt class="sql docutils literal"><span class="pre">...</span> <span class="pre">AS</span> <span class="pre">RANGE</span></tt>) can be
adapted to a custom <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> subclass:</p>
<dl class="function">
<dt id="psycopg2.extras.register_range">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_range</tt><big>(</big><em>pgrange</em>, <em>pyrange</em>, <em>conn_or_curs</em>, <em>globally=False</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_range" title="Permalink to this definition">¶</a></dt>
<dd><p>Create and register an adapter and the typecasters to convert between
a PostgreSQL <a class="reference external" href="http://www.postgresql.org/docs/current/static/rangetypes.html"><tt class="sql docutils literal"><span class="pre">range</span></tt></a> type and a PostgreSQL <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> subclass.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>pgrange</strong> &#8211; the name of the PostgreSQL <tt class="sql docutils literal"><span class="pre">range</span></tt> type. Can be
schema-qualified</li>
<li><strong>pyrange</strong> &#8211; a <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> strict subclass, or just a name to give to a new
class</li>
<li><strong>conn_or_curs</strong> &#8211; a connection or cursor used to find the oid of the
range and its subtype; the typecaster is registered in a scope limited
to this object, unless <em>globally</em> is set to <tt class="xref py py-obj docutils literal"><span class="pre">True</span></tt></li>
<li><strong>globally</strong> &#8211; if <tt class="xref py py-obj docutils literal"><span class="pre">False</span></tt> (default) register the typecaster only on
<em>conn_or_curs</em>, otherwise register it globally</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="#psycopg2.extras.RangeCaster" title="psycopg2.extras.RangeCaster"><tt class="xref py py-obj docutils literal"><span class="pre">RangeCaster</span></tt></a> instance responsible for the conversion</p>
</td>
</tr>
</tbody>
</table>
<p>If a string is passed to <em>pyrange</em>, a new <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> subclass is created
with such name and will be available as the <a class="reference internal" href="#psycopg2.extras.RangeCaster.range" title="psycopg2.extras.RangeCaster.range"><tt class="xref py py-obj docutils literal"><span class="pre">range</span></tt></a> attribute
of the returned <a class="reference internal" href="#psycopg2.extras.RangeCaster" title="psycopg2.extras.RangeCaster"><tt class="xref py py-obj docutils literal"><span class="pre">RangeCaster</span></tt></a> object.</p>
<p>The function queries the database on <em>conn_or_curs</em> to inspect the
<em>pgrange</em> type and raises <a class="reference internal" href="module.html#psycopg2.ProgrammingError" title="psycopg2.ProgrammingError"><tt class="xref py py-obj docutils literal"><span class="pre">ProgrammingError</span></tt></a> if the type is not
found.  If querying the database is not advisable, use directly the
<a class="reference internal" href="#psycopg2.extras.RangeCaster" title="psycopg2.extras.RangeCaster"><tt class="xref py py-obj docutils literal"><span class="pre">RangeCaster</span></tt></a> class and register the adapter and typecasters using the
provided functions.</p>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.RangeCaster">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">RangeCaster</tt><big>(</big><em>pgrange</em>, <em>pyrange</em>, <em>oid</em>, <em>subtype_oid</em>, <em>array_oid=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.RangeCaster" title="Permalink to this definition">¶</a></dt>
<dd><p>Helper class to convert between <a class="reference internal" href="#psycopg2.extras.Range" title="psycopg2.extras.Range"><tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt></a> and PostgreSQL range types.</p>
<p>Objects of this class are usually created by <a class="reference internal" href="#psycopg2.extras.register_range" title="psycopg2.extras.register_range"><tt class="xref py py-obj docutils literal"><span class="pre">register_range()</span></tt></a>. Manual
creation could be useful if querying the database is not advisable: in
this case the oids must be provided.</p>
<p>Object attributes:</p>
<dl class="attribute">
<dt id="psycopg2.extras.RangeCaster.range">
<tt class="descname">range</tt><a class="headerlink" href="#psycopg2.extras.RangeCaster.range" title="Permalink to this definition">¶</a></dt>
<dd><p>The <tt class="xref py py-obj docutils literal"><span class="pre">Range</span></tt> subclass adapted.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.RangeCaster.adapter">
<tt class="descname">adapter</tt><a class="headerlink" href="#psycopg2.extras.RangeCaster.adapter" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="extensions.html#psycopg2.extensions.ISQLQuote" title="psycopg2.extensions.ISQLQuote"><tt class="xref py py-obj docutils literal"><span class="pre">ISQLQuote</span></tt></a> responsible to adapt <tt class="xref py py-obj docutils literal"><span class="pre">range</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.RangeCaster.typecaster">
<tt class="descname">typecaster</tt><a class="headerlink" href="#psycopg2.extras.RangeCaster.typecaster" title="Permalink to this definition">¶</a></dt>
<dd><p>The object responsible for casting.</p>
</dd></dl>

<dl class="attribute">
<dt id="psycopg2.extras.RangeCaster.array_typecaster">
<tt class="descname">array_typecaster</tt><a class="headerlink" href="#psycopg2.extras.RangeCaster.array_typecaster" title="Permalink to this definition">¶</a></dt>
<dd><p>The object responsible to cast arrays, if available, else <tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="uuid-data-type">
<span id="adapt-uuid"></span><span id="index-8"></span><h3>UUID data type<a class="headerlink" href="#uuid-data-type" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.9.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.0.13: </span>added UUID array support.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">register_uuid</span><span class="p">()</span>
<span class="go">&lt;psycopg2._psycopg.type object at 0x...&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># Python UUID can be used in SQL queries</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">uuid</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_uuid</span> <span class="o">=</span> <span class="n">uuid</span><span class="o">.</span><span class="n">UUID</span><span class="p">(</span><span class="s">&#39;{12345678-1234-5678-1234-567812345678}&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extensions</span><span class="o">.</span><span class="n">adapt</span><span class="p">(</span><span class="n">my_uuid</span><span class="p">)</span><span class="o">.</span><span class="n">getquoted</span><span class="p">()</span>
<span class="go">&quot;&#39;12345678-1234-5678-1234-567812345678&#39;::uuid&quot;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># PostgreSQL UUID are transformed into Python UUID objects.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT &#39;a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11&#39;::uuid&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">UUID(&#39;a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11&#39;)</span>
</pre></div>
</div>
<dl class="function">
<dt id="psycopg2.extras.register_uuid">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_uuid</tt><big>(</big><em>oids=None</em>, <em>conn_or_curs=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_uuid" title="Permalink to this definition">¶</a></dt>
<dd><p>Create the UUID type and an uuid.UUID adapter.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>oids</strong> &#8211; oid for the PostgreSQL <tt class="sql docutils literal"><span class="pre">uuid</span></tt> type, or 2-items sequence
with oids of the type and the array. If not specified, use PostgreSQL
standard oids.</li>
<li><strong>conn_or_curs</strong> &#8211; where to register the typecaster. If not specified,
register it globally.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.UUID_adapter">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">UUID_adapter</tt><big>(</big><em>uuid</em><big>)</big><a class="headerlink" href="#psycopg2.extras.UUID_adapter" title="Permalink to this definition">¶</a></dt>
<dd><p>Adapt Python&#8217;s <a class="reference external" href="http://docs.python.org/library/uuid.html">uuid.UUID</a> type to PostgreSQL&#8217;s <a class="reference external" href="http://www.postgresql.org/docs/current/static/datatype-uuid.html">uuid</a>.</p>
</dd></dl>

</div>
<div class="section" id="inet-data-type">
<span id="index-9"></span><h3><tt class="sql docutils literal"><span class="pre">inet</span></tt> data type<a class="headerlink" href="#inet-data-type" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.9.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4.5: </span>added inet array support.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">psycopg2</span><span class="o">.</span><span class="n">extras</span><span class="o">.</span><span class="n">register_inet</span><span class="p">()</span>
<span class="go">&lt;psycopg2._psycopg.type object at 0x...&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">mogrify</span><span class="p">(</span><span class="s">&quot;SELECT </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">Inet</span><span class="p">(</span><span class="s">&#39;127.0.0.1/32&#39;</span><span class="p">),))</span>
<span class="go">&quot;SELECT E&#39;127.0.0.1/32&#39;::inet&quot;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT &#39;192.168.0.1/24&#39;::inet&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">addr</span>
<span class="go">&#39;192.168.0.1/24&#39;</span>
</pre></div>
</div>
<dl class="function">
<dt id="psycopg2.extras.register_inet">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_inet</tt><big>(</big><em>oid=None</em>, <em>conn_or_curs=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_inet" title="Permalink to this definition">¶</a></dt>
<dd><p>Create the INET type and an Inet adapter.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>oid</strong> &#8211; oid for the PostgreSQL <tt class="sql docutils literal"><span class="pre">inet</span></tt> type, or 2-items sequence
with oids of the type and the array. If not specified, use PostgreSQL
standard oids.</li>
<li><strong>conn_or_curs</strong> &#8211; where to register the typecaster. If not specified,
register it globally.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="class">
<dt id="psycopg2.extras.Inet">
<em class="property">class </em><tt class="descclassname">psycopg2.extras.</tt><tt class="descname">Inet</tt><big>(</big><em>addr</em><big>)</big><a class="headerlink" href="#psycopg2.extras.Inet" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrap a string to allow for correct SQL-quoting of inet values.</p>
<p>Note that this adapter does NOT check the passed value to make
sure it really is an inet-compatible address but DOES call adapt()
on it to make sure it is impossible to execute an SQL-injection
by passing an evil value to the initializer.</p>
</dd></dl>

</div>
</div>
<div class="section" id="fractional-time-zones">
<span id="index-10"></span><h2>Fractional time zones<a class="headerlink" href="#fractional-time-zones" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="psycopg2.extras.register_tstz_w_secs">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">register_tstz_w_secs</tt><big>(</big><em>oids=None</em>, <em>conn_or_curs=None</em><big>)</big><a class="headerlink" href="#psycopg2.extras.register_tstz_w_secs" title="Permalink to this definition">¶</a></dt>
<dd><p>The function used to register an alternate type caster for
<tt class="sql docutils literal"><span class="pre">TIMESTAMP</span> <span class="pre">WITH</span> <span class="pre">TIME</span> <span class="pre">ZONE</span></tt> to deal with historical time zones with
seconds in the UTC offset.</p>
<p>These are now correctly handled by the default type caster, so currently
the function doesn&#8217;t do anything.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.9.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.2.2: </span>function is no-op: see <a class="reference internal" href="usage.html#tz-handling"><em>Time zones handling</em></a>.</p>
</div>
</dd></dl>

</div>
<div class="section" id="coroutine-support">
<span id="index-11"></span><h2>Coroutine support<a class="headerlink" href="#coroutine-support" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="psycopg2.extras.wait_select">
<tt class="descclassname">psycopg2.extras.</tt><tt class="descname">wait_select</tt><big>(</big><em>conn</em><big>)</big><a class="headerlink" href="#psycopg2.extras.wait_select" title="Permalink to this definition">¶</a></dt>
<dd><p>Wait until a connection or cursor has data available.</p>
<p>The function is an example of a wait callback to be registered with
<a class="reference internal" href="extensions.html#psycopg2.extensions.set_wait_callback" title="psycopg2.extensions.set_wait_callback"><tt class="xref py py-obj docutils literal"><span class="pre">set_wait_callback()</span></tt></a>. This function uses
<a class="reference external" href="http://docs.python.org/3.2/library/select.html#select.select" title="(in Python v3.2)"><tt class="xref py py-func docutils literal"><span class="pre">select()</span></tt></a> to wait for data available.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#"><tt class="docutils literal"><span class="pre">psycopg2.extras</span></tt> &#8211; Miscellaneous goodies for Psycopg 2</a><ul>
<li><a class="reference internal" href="#connection-and-cursor-subclasses">Connection and cursor subclasses</a><ul>
<li><a class="reference internal" href="#dictionary-like-cursor">Dictionary-like cursor</a></li>
<li><a class="reference internal" href="#real-dictionary-cursor">Real dictionary cursor</a></li>
<li><a class="reference internal" href="#namedtuple-cursor"><tt class="docutils literal"><span class="pre">namedtuple</span></tt> cursor</a></li>
<li><a class="reference internal" href="#logging-cursor">Logging cursor</a></li>
</ul>
</li>
<li><a class="reference internal" href="#additional-data-types">Additional data types</a><ul>
<li><a class="reference internal" href="#json-adaptation">JSON adaptation</a></li>
<li><a class="reference internal" href="#hstore-data-type">Hstore data type</a></li>
<li><a class="reference internal" href="#composite-types-casting">Composite types casting</a></li>
<li><a class="reference internal" href="#range-data-types">Range data types</a></li>
<li><a class="reference internal" href="#uuid-data-type">UUID data type</a></li>
<li><a class="reference internal" href="#inet-data-type"><tt class="sql docutils literal"><span class="pre">inet</span></tt> data type</a></li>
</ul>
</li>
<li><a class="reference internal" href="#fractional-time-zones">Fractional time zones</a></li>
<li><a class="reference internal" href="#coroutine-support">Coroutine support</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="pool.html"
                        title="previous chapter"><tt class="docutils literal"><span class="pre">psycopg2.pool</span></tt> &#8211; Connections pooling</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="errorcodes.html"
                        title="next chapter"><tt class="docutils literal docutils literal docutils literal"><span class="pre">psycopg2.errorcodes</span></tt> &#8211; Error codes defined by PostgreSQL</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/extras.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="errorcodes.html" title="psycopg2.errorcodes – Error codes defined by PostgreSQL"
             >next</a> |</li>
        <li class="right" >
          <a href="pool.html" title="psycopg2.pool – Connections pooling"
             >previous</a> |</li>
        <li><a href="index.html">Psycopg 2.5.4 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2001-2014, Federico Di Gregorio, Daniele Varrazzo.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
    </div>
  </body>
</html>