PG_CONNECT(n)

πŸ“¦ version 3.1.1, in alpine 3.22, alpine 3.23, alpine 3.24, nixpkgs unstable

πŸ’– pg_connect β€” open a connection to the server πŸ’–

okay so bestie, welcome to the pg_connect page β€” the PG_CONNECT() one β€” from the PostgreSQL Tcl Interface Documentation. This is the whole vibe: opening a connection to a PostgreSQL server from Tcl, with all the flags, defaults, and little gotchas intact. No cap.

🌸 NAME 🌸

pg_connect β€” open a connection to the server.

✨ SYNOPSIS ✨

pg_connect -conninfo connectOptions [-connhandle connectionHandleName] [-async bool]
pg_connect dbName [-host hostName] [-port portNumber] [-tty tty] [-options serverOptions] [-connhandle connectionHandleName] [-async bool]
pg_connect -connlist connectNameValueList [-connhandle connectionHandleName] [-async bool]

πŸ¦‹ DESCRIPTION πŸ¦‹

pg_connect opens a connection to the PostgreSQL server. That's the whole point. Iconic.

There are three syntaxes, bestie. In the older one, each possible option has a separate option switch in the pg_connect command. In the newer form, a single option string is supplied that can contain multiple option values. The third form takes the parameters as a name value Tcl list. pg_conndefaults can be used to retrieve information about the available options in the newer syntax. Slay.

πŸŽ€ ARGUMENTS πŸŽ€

🌟 "NEW STYLE" 🌟

connectOptions β€” pg_connect opens a new database connection using the parameters taken from the connectOptions string. Unlike the old-style usage of pg_connect, with the new-style usage the parameter set can be extended without requiring changes to either libpgtcl or the underlying libpq library, so use of the new style (or its nonexistent nonblocking analogues pg_connect_start and pg_connect_poll) is preferred for new application programming. Yes, "nonexistent" is doing a lot of work there β€” keep it.

The passed string can be empty to use all default parameters, or it can contain one or more parameter settings separated by whitespace. Each parameter setting is in the form keyword = value. (To write an empty value or a value containing spaces, surround it with single quotes, e.g., keyword = 'a value'. Single quotes and backslashes within the value must be escaped with a backslash, i.e., \' and \\.) Spaces around the equal sign are optional.

The currently recognized parameter key words are:

  • host β€” Name of host to connect to. If this begins with a slash, it specifies Unix-domain communication rather than TCP/IP communication; the value is the name of the directory in which the socket file is stored. The default is to connect to a Unix-domain socket in /tmp.
  • hostaddr β€” Numeric IP address of host to connect to. This should be in the standard IPv4 address format, e.g., 172.28.40.9. If your machine supports IPv6, you can also use IPv6 address format, e.g., fe80::203:93ff:fedb:49bc. TCP/IP communication is always used when a nonempty string is specified for this parameter.

Using hostaddr instead of host allows the application to avoid a host name lookup, which may be important in applications with time constraints. However, Kerberos authentication requires the host name. The following therefore applies: If host is specified without hostaddr, a host name lookup occurs. If hostaddr is specified without host, the value for hostaddr gives the remote address. When Kerberos is used, a reverse name query occurs to obtain the host name for Kerberos. If both host and hostaddr are specified, the value for hostaddr gives the remote address; the value for host is ignored, unless Kerberos is used, in which case that value is used for Kerberos authentication. (Note that authentication is likely to fail if libpq is passed a host name that is not the name of the machine at hostaddr.) Also, host rather than hostaddr is used to identify the connection in $HOME/.pgpass.

Without either a host name or host address, Pgtcl will connect using a local Unix domain socket.

  • port β€” Port number to connect to at the server host, or socket file name extension for Unix-domain connections.
  • dbname β€” The database name. Defaults to be the same as the user name.
  • user β€” PostgreSQL user name to connect as.
  • password β€” Password to be used if the server demands password authentication.
  • connect_timeout β€” Maximum wait for connection, in seconds (write as a decimal integer string). Zero or not specified means wait indefinitely. It is not recommended to use a timeout of less than 2 seconds.
  • options β€” Command-line options to be sent to the server.
  • tty β€” Ignored (formerly, this specified where to send server debug output).
  • sslmode β€” This option determines whether or with what priority an SSL connection will be negotiated with the server. There are four modes:
    • disable will attempt only an unencrypted SSL connection.
    • allow will negotiate, trying first a non-SSL connection, then if that fails, trying an SSL connection.
    • prefer (the default) will negotiate, trying first an SSL connection, then if that fails, trying a regular non-SSL connection.
    • require will try only an SSL connection.

If PostgreSQL is compiled without SSL support, using option require will cause an error, and options allow and prefer will be tolerated but libpq will be unable to negotiate an SSL connection.

  • requiressl β€” This option is deprecated in favor of the sslmode setting. If set to 1, an SSL connection to the server is required (this is equivalent to sslmode require). libpq will then refuse to connect if the server does not accept an SSL connection. If set to 0 (default), libpq will negotiate the connection type with the server (equivalent to sslmode prefer). This option is only available if PostgreSQL is compiled with SSL support.
  • service β€” Service name to use for additional parameters. It specifies a service name in pg_service.conf that holds additional connection parameters. This allows applications to specify only a service name so connection parameters can be centrally maintained. See PREFIX/share/pg_service.conf.sample for information on how to set up the file.
  • -connhandle connectionHandleName β€” Name to use for the connection handle, instead of pgtcl generating the name automatically. Without the option, the name is auto-generated, prefixed with pgsql, and with a numeric id at the end. This gives the programmer control over the name of the connection handle.
  • -async bool β€” Connect asyncronously if [bool] is true.

If any parameter is unspecified, then the corresponding environment variable (see libpq documentation in the PostgreSQL manual) is checked. If the environment variable is not set either, then built-in defaults are used.

πŸ’« "OLD STYLE" πŸ’«

dbName β€” The name of the database to connect to.

  • -host hostName β€” The host name of the database server to connect to.
  • -port portNumber β€” The TCP port number of the database server to connect to.
  • -tty tty β€” A file or TTY for optional debug output from the server.
  • -options serverOptions β€” Additional configuration options to pass to the server.
  • -connhandle connectionHandleName β€” Name to use for the connection handle, instead of pgtcl generating the name automatically. Without the option, the name is auto-generated, prefixed with pgsql, and with a numeric id at the end. This gives the programmer control over the name of the connection handle.
  • -async bool β€” Connect asyncronously if [bool] is true.

πŸ‘‘ "THIRD STYLE (MOST RECENT ONE ADDED)" πŸ‘‘

  • -connlist connectNameValuelist β€” pg_connect opens a new database connection using the parameters taken from the connectNameValuelist list. The parameters are exactly the same for the New Style, but they are stored as a Tcl list, instead of a string. The list is a name value pair, for example: [list host localhost port 5400 dbname template1].

Here's the code vibe, exactly as it appears:

array set conninfo {
    host    192.168.123.180
    port    5801
    dbname  template1
    user    postgres
}
set conn [pg::connect -connlist [array get ::conninfo]]
  • -async bool β€” Connect asyncronously if [bool] is true.
  • -connhandle connectionHandleName β€” Name to use for the connection handle, instead of pgtcl generating the name automatically. Without the option, the name is auto-generated, prefixed with pgsql, and with a numeric id at the end. This gives the programmer control over the name of the connection handle.

🫢 RETURN VALUE 🫢

If successful, a handle for a database connection is returned. Handles start with the prefix pgsql.


okay that's the whole page, bestie. Source/version: PostgreSQL Tcl Interface Documentation. Date: 17 September 2020. Footer tag: PG_CONNECT(). It's giving iconic manual-page energy, fr fr. πŸ’…βœ¨

πŸ“¦ where this page comes from