API¶
pgdumplib¶
pgdumplib
¶
pgdumplib exposes a load method to create a :py:class:~pgdumplib.dump.Dump
instance from a :command:pg_dump file created in the custom format.
See the :doc:examples page to see how to read a dump or create one.
load(filepath, converter=None)
¶
Load a pg_dump file from disk
:param filepath: The path to the dump to load
:type filepath: str or pathlib.Path
:param converter: The data converter class to use
(Default: :py:class:pgdumplib.converters.DataConverter)
:type converter: Converter class or None
:raises: :py:exc:ValueError
:rtype: pgdumplib.dump.Dump
Source code in pgdumplib/__init__.py
new(dbname='pgdumplib', encoding='UTF8', converter=None, appear_as='18.0')
¶
Create a new :py:class:pgdumplib.dump.Dump instance
:param dbname: The database name for the dump (Default: pgdumplib)
:param encoding: The data encoding (Default: UTF8)
:param converter: The data converter class to use
(Default: :py:class:pgdumplib.converters.DataConverter)
:type converter: Converter class or None
:param appear_as: The version of Postgres to emulate
(Default: 18.0)
:rtype: pgdumplib.dump.Dump
Source code in pgdumplib/__init__.py
pgdumplib.dump¶
pgdumplib.dump
¶
The :py:class:~pgdumplib.dump.Dump class exposes methods to
:py:meth:load <pgdumplib.dump.Dump.load> an existing dump,
to :py:meth:add an entry <pgdumplib.dump.Dump.add_entry> to a dump,
to :py:meth:add table data <pgdumplib.dump.Dump.add_data> to a dump,
to :py:meth:add blob data <pgdumplib.dump.Dump.add_blob> to a dump,
and to :py:meth:save <pgdumplib.dump.Dump.save> a new dump.
There are :doc:converters that are available to format the data that is
returned by :py:meth:~pgdumplib.dump.Dump.read_data. The converter
is passed in during construction of a new :py:class:~pgdumplib.dump.Dump,
and is also available as an argument to :py:func:pgdumplib.load.
The default converter, :py:class:~pgdumplib.converters.DataConverter will
return all fields as strings, only replacing NULL with
:py:const:None. The :py:class:~pgdumplib.converters.SmartDataConverter
will attempt to convert all columns to native Python data types.
TableData
¶
Used to encapsulate table data allowing for the appending of data one row at a time.
Do not create this class directly, instead invoke
:py:meth:~pgdumplib.dump.Dump.table_data_writer.
Source code in pgdumplib/dump.py
size
property
¶
Return the current size of the buffered data
append(*args)
¶
Append a row to the table data, passing columns in as args
Column order must match the order specified when
:py:meth:~pgdumplib.dump.Dump.table_data_writer was invoked.
All columns will be coerced to a string with special attention
paid to None, converting it to the null marker (\N) and
:py:class:datetime.datetime objects, which will have the proper
pg_dump timestamp format applied to them.
Source code in pgdumplib/dump.py
Dump
¶
Create a new instance of the :py:class:~pgdumplib.dump.Dump class
Once created, the instance of :py:class:~pgdumplib.dump.Dump can
be used to read existing dumps or to create new ones.
:param str dbname: The database name for the dump
(Default: pgdumplib)
:param str encoding: The data encoding (Default: UTF8)
:param converter: The data converter class to use
(Default: :py:class:pgdumplib.converters.DataConverter)
:param str appear_as: The version of Postgres to emulate
(Default: 12.0)
Source code in pgdumplib/dump.py
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 | |
compression_algorithm
property
¶
The compression algorithm used in the dump
dbname
property
¶
The database name
dump_version
property
¶
The pg_dump version string
entries
property
¶
The list of entries in the dump
format
property
writable
¶
The dump format (Custom, Directory, Tar)
server_version
property
¶
The PostgreSQL server version string
timestamp
property
¶
The dump creation timestamp
version
property
¶
The archive format version as a tuple
add_entry(desc, namespace=None, tag=None, owner=None, defn=None, drop_stmt=None, copy_stmt=None, dependencies=None, tablespace=None, tableam=None, dump_id=None)
¶
Add an entry to the dump
A :py:exc:ValueError will be raised if desc is not value that
is known in :py:module:pgdumplib.constants.
When adding data, use :py:meth:~Dump.table_data_writer instead
of invoking :py:meth:~Dump.add_entry directly.
If dependencies are specified, they will be validated and if a
dump_id is specified and no entry is found with that
dump_id, a :py:exc:ValueError will be raised.
Other omitted values will be set to the default values specified
in the :py:class:pgdumplib.dump.Entry class.
The dump_id will be auto-calculated based upon the existing
entries if it is not specified.
.. note:: The creation of ad-hoc blobs is not supported.
:param str desc: The entry description
:param str namespace: The namespace of the entry
:param str tag: The name/table/relation/etc of the entry
:param str owner: The owner of the object in Postgres
:param str defn: The DDL definition for the entry
:param drop_stmt: A drop statement used to drop the entry
:param copy_stmt: A copy statement used when there is a
corresponding data section.
:param list dependencies: A list of dump_ids of objects that
the entry is dependent upon.
:param str tablespace: The tablespace to use
:param str tableam: The table access method
:param int dump_id: The dump id, will be auto-calculated if
left empty
:raises: :py:exc:ValueError
:rtype: pgdumplib.dump.Entry
Source code in pgdumplib/dump.py
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 | |
blobs()
¶
Iterator that returns each blob in the dump
:rtype: tuple(int, bytes)
get_entry(dump_id)
¶
Return the entry for the given dump_id
:param int dump_id: The dump ID of the entry to return.
Source code in pgdumplib/dump.py
load(path)
¶
Load the Dumpfile
.. note::
Loaded dumps are saved without compression by default,
regardless of the original compression setting. The
original compression algorithm is preserved for
inspection via :attr:compression_algorithm. Use
:meth:set_compression after loading to specify the
desired output compression.
:param os.PathLike path: The path of the dump to load
:raises: :py:exc:RuntimeError
:raises: :py:exc:ValueError
Source code in pgdumplib/dump.py
lookup_entry(desc, namespace, tag)
¶
Lookup an entry by description, namespace, and tag
:param str desc: The entry description :param str namespace: The namespace :param str tag: The tag :rtype: pgdumplib.models.Entry or None
Source code in pgdumplib/dump.py
save(path)
¶
set_compression(algorithm)
¶
Set the compression algorithm
:param str algorithm: The compression algorithm (none, gzip, lz4, zstd)
set_format(fmt)
¶
Set the output format
:param str fmt: The output format (Custom, Directory, Tar)
table_data(namespace, table)
¶
Iterate over the data for the specified table
:param str namespace: The namespace/schema
:param str table: The table name
:raises: :py:exc:pgdumplib.exceptions.EntityNotFoundError
:raises: :py:exc:pgdumplib.exceptions.NoDataError
Source code in pgdumplib/dump.py
table_data_writer(entry, columns)
¶
Context manager for writing table data
:param entry: The entry the data is for :param columns: The columns being written :rtype: TableData