Pike v8.0 release 1738

Method Sql.pgsql()->create()


Method create

Sql.pgsql Sql.pgsql(void|string host, void|string database, void|string user, void|string pass, void|mapping(string:mixed) options)

Description

With no arguments, this function initialises a connection to the PostgreSQL backend. Since PostgreSQL requires a database to be selected, it will try to connect to the default database. The connection may fail however, for a variety of reasons; in this case the most likely reason is because you don't have sufficient privileges to connect to that database. So use of this particular syntax is discouraged.

Parameter host

Should either contain "hostname" or "hostname:portname". This allows you to specify the TCP/IP port to connect to. If the parameter is 0 or "", it will try to connect to localhost, default port.

Parameter database

Specifies the database to connect to. Not specifying this is only supported if the PostgreSQL backend has a default database configured. If you do not want to connect to any live database, you can use "template1".

Parameter options

Currently supports at least the following:

"reconnect" : int

Set it to zero to disable automatic reconnects upon losing the connection to the database. Not setting it, or setting it to one, will cause one timed reconnect to take place. Setting it to -1 will cause the system to try and reconnect indefinitely.

"use_ssl" : int

If the database supports and allows SSL connections, the session will be SSL encrypted, if not, the connection will fallback to plain unencrypted.

"force_ssl" : int

If the database supports and allows SSL connections, the session will be SSL encrypted, if not, the connection will abort.

"text_query" : int

Send queries to and retrieve results from the database using text instead of the, generally more efficient, default native binary method. Turning this on will allow multiple statements per query separated by semicolons (not recommended).

"sync_parse" : int

Set it to zero to turn synchronous parsing off for statements. Setting this to off can cause surprises because statements could be parsed before the previous statements have been executed (e.g. references to temporary tables created in the preceding statement), but it can speed up parsing due to increased parallelism.

"cache_autoprepared_statements" : int

If set to one, it enables the automatic statement prepare and cache logic; caching prepared statements can be problematic when stored procedures and tables are redefined which leave stale references in the already cached prepared statements. The default is off, because PostgreSQL 10.1 (at least) has a bug that makes it spike to 100% CPU sometimes when this is on.

"client_encoding" : string

Character encoding for the client side, it defaults to using the default encoding specified by the database, e.g. "UTF8" or "SQL_ASCII".

"standard_conforming_strings" : string

When on, backslashes in strings must not be escaped any longer, quote() automatically adjusts quoting strategy accordingly.

"escape_string_warning" : string

When on, a warning is issued if a backslash (\) appears in an ordinary string literal and "standard_conforming_strings" is off, defaults to on.

For the numerous other options please check the PostgreSQL manual.

Note

You need to have a database selected before using the SQL-object, otherwise you'll get exceptions when you try to query it. Also notice that this function can raise exceptions if the db server doesn't respond, if the database doesn't exist or is not accessible to you.

Note

It is possible that the exception from a failed connect will not be triggered on this call (because the connect proceeds asynchronously in the background), but on the first attempt to actually use the database instead.

See also

Postgres.postgres, Sql.Sql, select_db(), https://www.postgresql.org/docs/current/static/runtime-config-client.html