|
| 1 | +Commands |
| 2 | +******** |
| 3 | + |
| 4 | +This chapter discusses the usage of the available console scripts. |
| 5 | + |
| 6 | + |
| 7 | +postgresql.bin.pg_python |
| 8 | +======================== |
| 9 | + |
| 10 | +The ``pg_python`` command provides a simple way to write Python scripts against a |
| 11 | +single target database. It acts like the regular Python console command, but |
| 12 | +takes standard PostgreSQL options as well to specify the client parameters |
| 13 | +to make establish connection with. The Python environment is then augmented |
| 14 | +with the following built-ins: |
| 15 | + |
| 16 | + ``db`` |
| 17 | + The PG-API connection object. |
| 18 | + |
| 19 | + ``xact`` |
| 20 | + ``db.xact``, the transaction creator. |
| 21 | + |
| 22 | + ``settings`` |
| 23 | + ``db.settings`` |
| 24 | + |
| 25 | + ``prepare`` |
| 26 | + ``db.prepare``, the statement creator. |
| 27 | + |
| 28 | + ``proc`` |
| 29 | + ``db.proc`` |
| 30 | + |
| 31 | + ``do`` |
| 32 | + ``db.do``, execute a single DO statement. |
| 33 | + |
| 34 | + ``sqlexec`` |
| 35 | + ``db.execute``, execute multiple SQL statements (``None`` is always returned) |
| 36 | + |
| 37 | +pg_python Usage |
| 38 | +--------------- |
| 39 | + |
| 40 | +Usage: postgresql.bin.pg_python [connection options] [script] ... |
| 41 | + |
| 42 | +Options: |
| 43 | + --unix=UNIX path to filesystem socket |
| 44 | + --ssl-mode=SSLMODE SSL requirement for connectivity: require, prefer, |
| 45 | + allow, disable |
| 46 | + -s SETTINGS, --setting=SETTINGS |
| 47 | + run-time parameters to set upon connecting |
| 48 | + -I PQ_IRI, --iri=PQ_IRI |
| 49 | + database locator string |
| 50 | + [pq://user:password@host:port/database?setting=value] |
| 51 | + -h HOST, --host=HOST database server host |
| 52 | + -p PORT, --port=PORT database server port |
| 53 | + -U USER, --username=USER |
| 54 | + user name to connect as |
| 55 | + -W, --password prompt for password |
| 56 | + -d DATABASE, --database=DATABASE |
| 57 | + database's name |
| 58 | + --pq-trace=PQ_TRACE trace PQ protocol transmissions |
| 59 | + -C PYTHON_CONTEXT, --context=PYTHON_CONTEXT |
| 60 | + Python context code to run[file://,module:,<code>] |
| 61 | + -m PYTHON_MAIN Python module to run as script(__main__) |
| 62 | + -c PYTHON_MAIN Python expression to run(__main__) |
| 63 | + --version show program's version number and exit |
| 64 | + --help show this help message and exit |
| 65 | + |
| 66 | + |
| 67 | +Interactive Console Backslash Commands |
| 68 | +-------------------------------------- |
| 69 | + |
| 70 | +Inspired by ``psql``:: |
| 71 | + |
| 72 | + >>> \? |
| 73 | + Backslash Commands: |
| 74 | + |
| 75 | + \? Show this help message. |
| 76 | + \E Edit a file or a temporary script. |
| 77 | + \e Edit and Execute the file directly in the context. |
| 78 | + \i Execute a Python script within the interpreter's context. |
| 79 | + \set Configure environment variables. \set without arguments to show all |
| 80 | + \x Execute the Python command within this process. |
| 81 | + |
| 82 | + |
| 83 | +pg_python Examples |
| 84 | +------------------ |
| 85 | + |
| 86 | +Module execution taking advantage of the new built-ins:: |
| 87 | + |
| 88 | + $ python3 -m postgresql.bin.pg_python -h localhost -W -m timeit "prepare('SELECT 1').first()" |
| 89 | + Password for pg_python[pq://jwp@localhost:5432]: |
| 90 | + 1000 loops, best of 3: 1.35 msec per loop |
| 91 | + |
| 92 | + $ python3 -m postgresql.bin.pg_python -h localhost -W -m timeit -s "ps=prepare('SELECT 1')" "ps.first()" |
| 93 | + Password for pg_python[pq://jwp@localhost:5432]: |
| 94 | + 1000 loops, best of 3: 442 usec per loop |
| 95 | + |
| 96 | +Simple interactive usage:: |
| 97 | + |
| 98 | + $ python3 -m postgresql.bin.pg_python -h localhost -W |
| 99 | + Password for pg_python[pq://jwp@localhost:5432]: |
| 100 | + >>> ps = prepare('select 1') |
| 101 | + >>> ps.first() |
| 102 | + 1 |
| 103 | + >>> c = ps() |
| 104 | + >>> c.read() |
| 105 | + [(1,)] |
| 106 | + >>> ps.close() |
| 107 | + >>> import sys |
| 108 | + >>> sys.exit(0) |
| 109 | + |
| 110 | + |
| 111 | +postgresql.bin.pg_dotconf |
| 112 | +========================= |
| 113 | + |
| 114 | +pg_dotconf is used to modify a PostgreSQL cluster's configuration file. |
| 115 | +It provides a means to apply settings specified from the command line and from a |
| 116 | +file referenced using the ``-f`` option. |
| 117 | + |
| 118 | +.. warning:: |
| 119 | + ``include`` directives in configuration files are *completely* ignored. If |
| 120 | + modification of an included file is desired, the command must be applied to |
| 121 | + that specific file. |
| 122 | + |
| 123 | + |
| 124 | +pg_dotconf Usage |
| 125 | +---------------- |
| 126 | + |
| 127 | +Usage: postgresql.bin.pg_dotconf [--stdout] [-f filepath] postgresql.conf ([param=val]|[param])* |
| 128 | + |
| 129 | +Options: |
| 130 | + --version show program's version number and exit |
| 131 | + -h, --help show this help message and exit |
| 132 | + -f SETTINGS, --file=SETTINGS |
| 133 | + A file of settings to *apply* to the given |
| 134 | + "postgresql.conf" |
| 135 | + --stdout Redirect the product to standard output instead of |
| 136 | + writing back to the "postgresql.conf" file |
| 137 | + |
| 138 | + |
| 139 | +Examples |
| 140 | +-------- |
| 141 | + |
| 142 | +Modifying a simple configuration file:: |
| 143 | + |
| 144 | + $ echo "setting = value" >pg.conf |
| 145 | + |
| 146 | + # change 'setting' |
| 147 | + $ python3 -m postgresql.bin.pg_dotconf pg.conf setting=newvalue |
| 148 | + |
| 149 | + $ cat pg.conf |
| 150 | + setting = 'newvalue' |
| 151 | + |
| 152 | + # new settings are appended to the file |
| 153 | + $ python3 -m postgresql.bin.pg_dotconf pg.conf another_setting=value |
| 154 | + $ cat pg.conf |
| 155 | + setting = 'newvalue' |
| 156 | + another_setting = 'value' |
| 157 | + |
| 158 | + # comment a setting |
| 159 | + $ python3 -m postgresql.bin.pg_dotconf pg.conf another_setting |
| 160 | + |
| 161 | + $ cat pg.conf |
| 162 | + setting = 'newvalue' |
| 163 | + #another_setting = 'value' |
| 164 | + |
| 165 | +When a setting is given on the command line, it must been seen as one argument |
| 166 | +to the command, so it's *very* important to avoid invocations like:: |
| 167 | + |
| 168 | + $ python3 -m postgresql.bin.pg_dotconf pg.conf setting = value |
| 169 | + ERROR: invalid setting, '=' after 'setting' |
| 170 | + HINT: Settings must take the form 'setting=value' or 'setting_name_to_comment'. Settings must also be received as a single argument. |
0 commit comments