### Example: Execute Query and Get Row Count Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Demonstrates how to establish a connection, execute a SQL query, and then retrieve and print the number of rows returned by the query. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT * FROM users LIMIT 10") println("Rows returned: $(LibPQ.num_rows(result))") ``` -------------------------------- ### LibPQ.jl Connection String Examples Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md Demonstrates various ways to establish a connection using LibPQ.jl's Connection constructor with different connection string formats and options. Includes examples for local connections, connections with credentials, and connections with URL parameters. Also shows how to use environment variables for connection details. ```julia using LibPQ # Local connection with defaults conn = LibPQ.Connection("postgresql://localhost/mydb") ``` ```julia # Connection with credentials conn = LibPQ.Connection("postgresql://user:password@host:5432/mydb") ``` ```julia # Connection with options conn = LibPQ.Connection( "postgresql://user:password@host/mydb?sslmode=require&application_name=MyApp" ) ``` ```julia # Environment variables (standard PostgreSQL) # PGHOST=localhost # PGPORT=5432 # PGDATABASE=mydb # PGUSER=myuser # PGPASSWORD=mypassword conn = LibPQ.Connection("postgresql://") # Uses environment variables ``` -------------------------------- ### Example: Fetching and Processing Query Results Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md A complete example demonstrating how to establish a connection, execute a query, and iterate through the results, accessing columns by index, name, and property. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT id, name, email FROM users LIMIT 1") for row in result println("ID: $(row[1])") println("Name: $(row.name)") println("Email: $(row.email)") end ``` -------------------------------- ### Implement a Simple Connection Pool Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md This example demonstrates a basic mutable struct for managing a pool of LibPQ connections. It includes functions to get and return connections from the pool. ```julia using LibPQ # Simple connection pool (example) mutable struct ConnectionPool connections::Vector{LibPQ.Connection} available::Vector{Int} function ConnectionPool(conn_string, size=5) connections = [LibPQ.Connection(conn_string) for _ in 1:size] return new(connections, 1:size) end end function get_connection(pool) if isempty(pool.available) error("No available connections") end idx = pop!(pool.available) return pool.connections[idx] end function return_connection(pool, idx) push!(pool.available, idx) end ``` -------------------------------- ### Example: Iterating Through All Columns and Their Values Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Provides a comprehensive example of fetching query results, accessing the collection of columns, and then iterating through each column to print its name and all its values. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT id, name, email FROM users LIMIT 5") columns = LibPQ.Tables.columns(result) for col in columns println("Column: $(col.col_name)") for value in col println(" $value") end end ``` -------------------------------- ### Example: Accessing and Iterating Through Columns Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Provides a complete example of connecting to a PostgreSQL database, executing a query, and then accessing and printing data from specific columns by index and name. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT * FROM users LIMIT 5") columns = LibPQ.Tables.columns(result) id_column = columns[1] name_column = columns[:name] for i in 1:length(id_column) println("User $(id_column[i]): $(name_column[i])") end ``` -------------------------------- ### Execute PostgreSQL Queries with LibPQ.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/index.md Demonstrates how to establish a connection, execute SQL queries, and retrieve results. Includes examples for parameterized queries and asynchronous execution. ```julia using LibPQ, Tables conn = LibPQ.Connection("dbname=postgres host=localhost port=5432") result = execute(conn, "SELECT typname FROM pg_type WHERE oid = 16") data = columntable(result) # the same but with parameters result = execute(conn, "SELECT typname FROM pg_type WHERE oid = \"1\"", ["16"]) data = columntable(result) # the same but asynchronously async_result = async_execute(conn, "SELECT typname FROM pg_type WHERE oid = \"1\"", ["16"]) # do other things result = fetch(async_result) data = columntable(result) close(conn) ``` -------------------------------- ### Example: Prepare and Execute Statement Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Demonstrates preparing a SQL statement with parameters and executing it multiple times with different values. Ensure you have a PostgreSQL server running and a database named 'mydb'. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Prepare a statement with parameters stmt = LibPQ.prepare(conn, "SELECT * FROM users WHERE id = \$1 AND active = \$2") # Execute multiple times with different parameters result1 = LibPQ.execute(stmt, [1, true]) result2 = LibPQ.execute(stmt, [2, false]) ``` -------------------------------- ### Checking Connection Status using C API Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/utilities.md Example of establishing a connection and checking its status using the `PQstatus` C function from the libpq API. This is useful for verifying connection health programmatically. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Check connection status using C API status = LibPQ.libpq_c.PQstatus(conn.conn) if status == LibPQ.libpq_c.CONNECTION_OK println("Connected") end ``` -------------------------------- ### Default Type Conversions Example Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/pages/type-conversions.md Demonstrates the default type conversions when fetching data from PostgreSQL into a Julia DataFrame. Shows how various PostgreSQL types are mapped to their Julia equivalents. ```julia julia> df = DataFrame(execute(conn, "SELECT 1::int4, 'foo'::varchar, '{1.0, 2.1, 3.3}'::float8[], false, TIMESTAMP '2004-10-19 10:23:54'")) 1×5 DataFrames.DataFrame │ Row │ int4 │ varchar │ float8 │ bool │ timestamp │ ├─────┼──────┼─────────┼─────────────────┼───────┼─────────────────────┤ │ 1 │ 1 │ foo │ [1.0, 2.1, 3.3] │ false │ 2004-10-19T10:23:54 │ ``` -------------------------------- ### Handle PQConnectionError Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/exceptions.md Example of catching a `PQConnectionError` when attempting to establish a connection to a PostgreSQL database. This snippet shows how to access the specific error message. ```julia using LibPQ try conn = LibPQ.Connection("postgresql://invalid_host/mydb"; connect_timeout=5) catch err::LibPQ.Errors.PQConnectionError println("Connection failed: $(err.msg)") end ``` -------------------------------- ### Execute SQL Query Asynchronously with Parameters Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/queries.md Use this function to start an asynchronous query with parameter placeholders. Ensure parameters are provided as a vector or tuple. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Start async query with parameters async_result = LibPQ.async_execute( conn, "SELECT * FROM users WHERE status = \"1"", ["active"] ) # Wait for results result = LibPQ.handle_result(async_result) ``` -------------------------------- ### Load Data with Transaction Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/tables.md Demonstrates loading data into a PostgreSQL table within a transaction for improved performance and data integrity. Ensure to explicitly start and commit the transaction. ```julia using LibPQ, DataFrames conn = LibPQ.Connection("postgresql://localhost/mydb") df = DataFrame(...) # Wrap in transaction for better performance LibPQ.execute(conn, "BEGIN;") LibPQ.load!( df, conn, "INSERT INTO users (name, email) VALUES ( $1, $2 )" ) LibPQ.execute(conn, "COMMIT;") ``` -------------------------------- ### Handle Serialization Failure in LibPQ Transactions Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/errors.md This example demonstrates how to handle `SerializationFailure` errors in LibPQ by retrying the transaction up to a maximum number of attempts. It ensures that transactions are retried when a concurrent transaction conflict occurs, with a fallback to rethrowing the error after exhausting retries. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") max_retries = 3 for attempt in 1:max_retries try LibPQ.execute(conn, "BEGIN ISOLATION LEVEL SERIALIZABLE;") # Perform transaction operations LibPQ.execute(conn, "UPDATE account SET balance = balance - 100 WHERE id = 1") LibPQ.execute(conn, "UPDATE account SET balance = balance + 100 WHERE id = 2") LibPQ.execute(conn, "COMMIT;") break # Success catch err::LibPQ.Errors.SerializationFailure LibPQ.execute(conn, "ROLLBACK;") if attempt == max_retries println("Transaction failed after $max_retries retries") rethrow() else println("Retrying transaction (attempt $attempt/$max_retries)") end end end ``` -------------------------------- ### LibPQ Connection with Custom Options Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/utilities.md Demonstrates how to establish a LibPQ connection using custom options, overriding server defaults. Alternatively, passing empty options uses server defaults. ```julia using LibPQ # Use custom options custom_options = Dict( "DateStyle" => "ISO,YMD", "search_path" => "public,shared_schema", ) conn = LibPQ.Connection( "postgresql://localhost/mydb"; options=custom_options ) # Or use server defaults by passing empty options conn = LibPQ.Connection( "postgresql://localhost/mydb"; options=Dict{String, String}() ) ``` -------------------------------- ### Create and Use a PostgreSQL Connection Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md Demonstrates how to establish a connection to a PostgreSQL server using a connection string, execute a query, and then close the connection. ```julia using LibPQ # Create a connection conn = LibPQ.Connection("postgresql://user:password@host/dbname") # Use the connection result = LibPQ.execute(conn, "SELECT 1") # Clean up LibPQ.close(conn) ``` -------------------------------- ### Get PostgreSQL Error Class Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/exceptions.md Retrieve the error class from a PQResultError object. ```julia error_class(err::PQResultError{Class}) -> Errors.Class ``` -------------------------------- ### Get PostgreSQL Error Code Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/exceptions.md Retrieve the specific error code from a PQResultError object. ```julia error_code(err::PQResultError{_, Code}) -> Errors.ErrorCode ``` -------------------------------- ### Get the Number of Columns Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Illustrates how to find out the total number of columns present in a `Columns` collection. ```julia ncols = length(columns) ``` -------------------------------- ### Get a Column by Name Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Shows how to access a `Column` object from a `Columns` collection by its symbolic name. ```julia column = columns[:column_name] ``` -------------------------------- ### Connect, Prepare, Execute, and Close with DBInterface Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/utilities.md Demonstrates how to use the DBInterface.jl API to connect to a PostgreSQL database, prepare and execute a query, and close the connection. Ensure LibPQ and DBInterface are imported. ```julia using LibPQ, DBInterface # Connect via DBInterface db = DBInterface.connect(LibPQ.Connection, "postgresql://localhost/mydb") # Execute via DBInterface stmt = DBInterface.prepare(db, "SELECT * FROM users WHERE id = \"1\") result = DBInterface.execute(stmt, [42]) # Close DBInterface.close!(db) ``` -------------------------------- ### Get Number of Rows in a Column Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Shows how to determine the total number of rows present in a `Column`. ```julia nrows = length(column) ``` -------------------------------- ### Execute Prepared Statement Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/INDEX.md Shows how to prepare a SQL statement with a placeholder and then execute it with specific parameters. This is useful for preventing SQL injection and improving performance for repeated queries. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") stmt = LibPQ.prepare(conn, "SELECT * FROM users WHERE id = \$1") result = LibPQ.execute(stmt, [42]) ``` -------------------------------- ### Get Schema from Result Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/tables.md Retrieve the schema information from a `Result` object. This provides details about the columns and their types. ```julia using Tables result = LibPQ.execute(conn, "SELECT * FROM users") # Get schema schema = Tables.schema(result) ``` -------------------------------- ### Get a Column by Index Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Demonstrates how to retrieve a specific `Column` object from a `Columns` collection using its 1-based index. ```julia column = columns[1] ``` -------------------------------- ### Create and Use Custom PQConversions Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/types.md Demonstrates creating an empty PQConversions map, defining custom conversion functions for specific (Oid, Julia Type) pairs, and applying them in a LibPQ.execute call. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Create custom conversions conversions = LibPQ.PQConversions() # Map (Oid, Type) -> conversion function conversions[(LibPQ.oid(:int4), Int64)] = x -> parse(Int64, x) conversions[(LibPQ.oid(:text), String)] = x -> String(x) # Use in query result = LibPQ.execute( conn, "SELECT id, name FROM users", conversions=conversions ) ``` -------------------------------- ### Get All Column Names Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieve a vector of all column names from a Result object. This is useful for understanding the structure of the query results. ```julia function column_names(jl_result::Result) -> Vector{String} ``` -------------------------------- ### Get Number of Columns in a LibPQ Result Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieves the count of columns in the result set. This can be used to understand the structure of the data. ```julia function num_columns(jl_result::Result) -> Int ``` -------------------------------- ### Basic PostgreSQL Workflow Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/INDEX.md Demonstrates the fundamental steps of connecting to a PostgreSQL database, executing a SELECT query, iterating through results, and closing the connection. ```julia using LibPQ # 1. Connect conn = LibPQ.Connection("postgresql://localhost/mydb") # 2. Execute query result = LibPQ.execute(conn, "SELECT * FROM users LIMIT 10") # 3. Access results for row in result println("ID: $(row.id), Name: $(row.name)") end # 4. Close connection LibPQ.close(conn) ``` -------------------------------- ### Override LibPQ Defaults with Server Defaults Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md Demonstrates how to use PostgreSQL server defaults instead of LibPQ.jl defaults by providing an empty options dictionary when establishing a connection. ```julia using LibPQ # Use server defaults conn = LibPQ.Connection( "postgresql://localhost/mydb"; options=Dict{String, String}() ) ``` -------------------------------- ### Get Number of Rows in a LibPQ Result Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieves the count of rows returned by a query. This is useful for understanding the extent of the data fetched. ```julia function num_rows(jl_result::Result) -> Int ``` -------------------------------- ### Handle LibPQ Exceptions Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/exceptions.md Demonstrates how to catch any `LibPQException` that occurs during connection or query execution. This is useful for general error reporting. ```julia using LibPQ try conn = LibPQ.Connection("postgresql://localhost/mydb") LibPQ.execute(conn, "SELECT * FROM users") catch err::LibPQ.Errors.LibPQException println("LibPQ error: $err") end ``` -------------------------------- ### Get Column Julia Types Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieves the Julia data types for each column in a query result. This is useful for understanding how data will be represented in Julia. ```julia function column_types(jl_result::Result) -> Vector{Type} ``` -------------------------------- ### Direct Table Construction from Results Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/tables.md Shows how to use `LibPQ.Result` objects directly with any Tables.jl consumer, enabling seamless integration with data manipulation packages. Requires `LibPQ` and `Tables` packages. ```julia using LibPQ, Tables conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT * FROM users") # Custom table construction materialize(result) # Collect all rows into a table # Use with package expecting Tables interface # (DataFrames, JSON, CSV, Arrow, etc.) ``` -------------------------------- ### Run PostgreSQL Docker Container for Testing Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/pages/faq.md Use Docker to quickly set up a PostgreSQL server for testing LibPQ.jl. This command detaches the container and maps port 5432. ```sh docker run --detach --name test-libpqjl -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 postgres ``` -------------------------------- ### Get Query Status Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieve the execution status of a database query result. This is useful for determining if a query succeeded or failed at the database level. ```julia function status(jl_result::Result) -> libpq_c.ExecStatusType ``` -------------------------------- ### Using Environment Variables for PostgreSQL Connection Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md Illustrates how to set standard PostgreSQL environment variables in the shell and then use them to establish a connection in Julia with LibPQ.jl. ```bash # Set environment variables export PGHOST=localhost export PGPORT=5432 export PGDATABASE=mydb export PGUSER=myuser export PGTZ=America/New_York # Use environment variables in Julia julia> using LibPQ julia> conn = LibPQ.Connection("postgresql://\n") ``` -------------------------------- ### Get libpq.jl Connection Encoding Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Retrieves the client encoding name for an active database connection. Currently, this is always UTF8 for Julia connections. ```julia function encoding(jl_conn::Connection) -> String end ``` -------------------------------- ### PostgreSQL Connection with Do-Syntax Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Demonstrates using do-syntax with a PostgreSQL connection for automatic resource cleanup. Any operations within the block are performed on the established connection. ```julia result = LibPQ.Connection("postgresql://localhost/mydb") do conn LibPQ.execute(conn, "SELECT 1") end ``` -------------------------------- ### Get Transaction Status Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Retrieves the current in-transaction status of a PostgreSQL connection. The status code requires interpretation via PostgreSQL documentation. ```julia function transaction_status(jl_conn::Connection) -> libpq_c.PGTransactionStatusType ``` -------------------------------- ### Get Column Names from Statement Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Retrieves the names of all columns that will be returned by executing a prepared statement. Useful for understanding the structure of query results. ```julia function column_names(stmt::Statement) -> Vector{String} end ``` -------------------------------- ### Integrate LibPQ.jl with DBInterface.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/index.md Demonstrates connecting to and querying PostgreSQL databases using the generic DBInterface.jl package with LibPQ.jl connections. ```julia using LibPQ, DBInterface conn = DBInterface.connect(LibPQ.Connection, "dbname=postgres") res = DBInterface.execute(con, "SELECT * FROM table") DBInterface.close!(conn) ``` -------------------------------- ### Using BINARY Constant for Binary Format Results Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/utilities.md Illustrates how to use the `BINARY` constant to request results in binary format from `LibPQ.execute`. This can be more efficient for large datasets. ```julia const BINARY = true result = LibPQ.execute(conn, "SELECT ...", binary_format=LibPQ.BINARY) ``` -------------------------------- ### Execute SQL Query Asynchronously Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/queries.md Use this function to start an asynchronous query without parameters. Errors are not thrown directly but when `handle_result()` is called. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Start async query async_result = LibPQ.async_execute(conn, "SELECT * FROM large_table") # Do other work... println("Query is executing...") # Wait for results result = LibPQ.handle_result(async_result) for row in result println(row) end ``` -------------------------------- ### Execute a Prepared Statement Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md Illustrates the use of prepared statements for executing parameterized queries efficiently, allowing the same statement to be run multiple times with different values. ```julia stmt = LibPQ.prepare(conn, "SELECT * FROM users WHERE id = \$1") result = LibPQ.execute(stmt, [42]) ``` -------------------------------- ### Get Number of Columns in Row Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieves the total number of columns present in a `Row`. This can be used to determine the row's width or for loop bounds. ```julia ncols = length(row) ``` -------------------------------- ### Get Error Message from Connection Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Retrieves the most recent error message generated by an operation on the specified connection. Returns an empty string if no error has occurred. ```julia function error_message(jl_conn::Connection) -> String end ``` -------------------------------- ### Setting Connection Options Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md Customize PostgreSQL connection parameters such as application name, search path, and timeouts. Pass these as a dictionary to the Connection constructor. ```julia options = Dict( "application_name" => "MyApp", "search_path" => "public,shared", "work_mem" => "256MB", "statement_timeout" => "30000", # 30 seconds ) conn = LibPQ.Connection("postgresql://localhost/mydb"; options=options) ``` -------------------------------- ### Create and Use Custom PQTypeMap Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/types.md Demonstrates creating an empty PQTypeMap, adding custom Oid-to-Type mappings, and using it in a LibPQ.execute call to influence query result type conversion. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Create custom type map type_map = LibPQ.PQTypeMap() type_map[LibPQ.oid(:int4)] = Int64 # Map int4 OID to Int64 type_map[LibPQ.oid(:text)] = String # Use in query result = LibPQ.execute( conn, "SELECT id, name FROM users", type_map=type_map ) ``` -------------------------------- ### Get Number of Parameters in Prepared Statement Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Use `num_params` to find out how many parameters a prepared statement expects. This is useful for validating input before execution. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") stmt = LibPQ.prepare(conn, "INSERT INTO users (name, email, active) VALUES ( $1, $2, $3)") println("Expected parameters: $(LibPQ.num_params(stmt))") # Output: 3 ``` -------------------------------- ### Get Schema of a Result Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/tables.md Retrieves the Tables.jl schema from a LibPQ.jl Result. This includes column names and their types, with nullable columns represented as Union{T, Missing}. ```julia using LibPQ, Tables conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT id, name, email FROM users") schema = Tables.schema(result) println("Columns: $(schema.names)") println("Types: $(schema.types)") ``` -------------------------------- ### Basic LibPQ.jl Connection Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md Establishes a basic connection to a PostgreSQL database using a connection string. ```julia using LibPQ # Basic connection conn = LibPQ.Connection("postgresql://localhost/mydb") ``` -------------------------------- ### Accessing libpq C API Types and Functions Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/utilities.md Demonstrates direct access to libpq C types and functions through the `libpq_c` submodule. Useful for low-level control and introspection. ```julia using LibPQ # Access C types and functions LibPQ.libpq_c.CONNECTION_OK # Connection status constant LibPQ.libpq_c.PGRES_TUPLES_OK # Result status constant LibPQ.libpq_c.PQstatus # C function LibPQ.libpq_c.Oid # Type alias ``` -------------------------------- ### LibPQ.jl Connection with Custom Server Options Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md Connects to a PostgreSQL database and sets custom server options such as application name, search path, and work memory. ```julia using LibPQ # With custom server options options = Dict( "application_name" => "MyApp", "search_path" => "public,shared", "work_mem" => "256MB", ) conn = LibPQ.Connection( "postgresql://localhost/mydb"; options=options ) ``` -------------------------------- ### Get Column PostgreSQL OIDs Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieves the PostgreSQL Object Identifiers (OIDs) for each column in a query result. This can be useful for advanced database interactions or type mapping. ```julia function column_oids(jl_result::Result) -> Vector{Oid} ``` -------------------------------- ### Direct Table Construction Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/tables.md Illustrates how to use `LibPQ.Result` objects directly with any package that implements the Tables.jl interface. ```APIDOC ## Direct Table Construction Use Results directly with any Tables.jl consumer: ```julia using LibPQ, Tables conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT * FROM users") # Custom table construction materialize(result) # Collect all rows into a table # Use with package expecting Tables interface # (DataFrames, JSON, CSV, Arrow, etc.) ``` ``` -------------------------------- ### Check Connection Status Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/utilities.md Demonstrates how to check the connection status using LibPQ.jl. It compares the status to predefined constants for OK and BAD connections. ```julia using LibPQ conn_status = LibPQ.status(conn) if conn_status == LibPQ.libpq_c.CONNECTION_OK println("Connected") elseif conn_status == LibPQ.libpq_c.CONNECTION_BAD println("Connection failed") end ``` -------------------------------- ### Get Column Name by Index Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Retrieve the name of a column from a prepared statement using its 1-based index with `column_name`. This function throws a `BoundsError` if the index is invalid. ```julia function column_name(stmt::Statement, column_number::Integer) -> String ``` -------------------------------- ### Get Specific Error Field Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieve a specific field from the PostgreSQL error report for a given result. Use predefined constants like `LibPQ.libpq_c.PG_DIAG_SEVERITY` to specify the field. ```julia function error_field(jl_result::Result, field_code::Union{Char, Integer}) -> Union{String, Nothing} ``` ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") try result = LibPQ.execute(conn, "INVALID SQL;") catch result = LibPQ.execute(conn, "SELECT 1") severity = LibPQ.error_field(result, LibPQ.libpq_c.PG_DIAG_SEVERITY) println("Severity: $severity") end ``` -------------------------------- ### Handle LibPQ Permission Errors in Julia Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/errors.md Demonstrates how to catch specific LibPQ permission and authentication errors using a try-catch block in Julia. This is useful for gracefully handling cases where a user lacks necessary privileges or provides incorrect credentials. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") try LibPQ.execute(conn, "DROP TABLE protected_table") catch err::LibPQ.Errors.InsufficientPrivilege println("Permission denied: user does not have DROP privilege") catch err::LibPQ.Errors.InvalidPassword println("Authentication failed: check password") end ``` -------------------------------- ### Get Error Message Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/results.md Retrieve the error message associated with a query result. Set `verbose` to true for a more detailed message. Returns an empty string if no error occurred. ```julia function error_message(jl_result::Result; verbose::Bool=false) -> String ``` ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") try result = LibPQ.execute(conn, "INVALID SQL;") catch result = LibPQ.execute(conn, "SELECT 1") println(LibPQ.error_message(result)) end ``` -------------------------------- ### Get PostgreSQL Server Version Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Retrieves the PostgreSQL server version for a given connection. The version is returned as a `VersionNumber` object, with specific formatting for versions 10+ and 9.x. ```julia function server_version(jl_conn::Connection) -> VersionNumber ``` ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") version = LibPQ.server_version(conn) println("PostgreSQL version: $version") ``` -------------------------------- ### Construct and Use Connection with Callback Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Use this constructor to create a connection, execute a function with it, and ensure the connection is closed automatically afterward. It accepts connection string arguments and keyword arguments for the constructor. ```julia using LibPQ result = LibPQ.Connection("postgresql://localhost/mydb") do conn LibPQ.execute(conn, "SELECT COUNT(*) FROM users") end ``` -------------------------------- ### Utilities and Helpers Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/INDEX.md Utility functions, macros, and constants provided by LibPQ.jl. ```APIDOC ## Utilities and Helpers ### Description Provides various utility functions, macros, and constants for enhanced functionality. ### Macros - `@pqv_str(s)`: Parses a PostgreSQL version string. ### Locking - `lock(conn)`: Acquire a lock on the connection. - `unlock(conn)`: Release a lock on the connection. - `islocked(conn)`: Check if the connection is locked. ### Constants - `BINARY`, `TEXT`, `DEFAULT_CLIENT_TIME_ZONE`: Predefined constants for various settings. - `Parameter`: Type alias for query parameters. - `CONNECTION_OPTION_DEFAULTS`: Default connection options. ### Type Aliases - `Oid`: Alias for PostgreSQL Object Identifiers. - `ExecStatusType`: Alias for execution status types. - `ConnStatusType`: Alias for connection status types. - `PGTransactionStatusType`: Alias for transaction status types. ``` -------------------------------- ### Get PostgreSQL Connection Status Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Retrieve the current status of a PostgreSQL connection. This is useful for verifying if a connection is active and valid before performing operations. The status can be `CONNECTION_OK` or `CONNECTION_BAD`. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") conn_status = LibPQ.status(conn) if conn_status == LibPQ.libpq_c.CONNECTION_OK println("Connected successfully") end ``` -------------------------------- ### PostgreSQL Connection with Custom Options Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Connects to a PostgreSQL database and applies custom server options, such as application name or search path. ```julia options = Dict("application_name" => "MyApp", "search_path" => "public") conn = LibPQ.Connection("postgresql://localhost/mydb"; options=options) ``` -------------------------------- ### Handle Asynchronous Query Execution Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md Demonstrates how to initiate an asynchronous query and then wait for its result. This allows other work to be performed while the query is running. ```julia async_result = LibPQ.async_execute(conn, "SELECT * FROM huge_table") # Do other work... result = LibPQ.handle_result(async_result) ``` -------------------------------- ### Get Number of Affected Rows Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/queries.md Use `num_affected_rows` after executing an UPDATE, INSERT, or DELETE query to determine how many rows were modified. This function returns 0 for SELECT queries. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute( conn, "UPDATE users SET active = true WHERE status = \"pending\" ["pending"] ) println("Updated $(LibPQ.num_affected_rows(result)) rows") ``` -------------------------------- ### Insert Data into PostgreSQL with LibPQ.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/index.md Shows how to insert data into a temporary table using `LibPQ.load!`. Handles both non-nullable and nullable columns, including `missing` values. ```julia using LibPQ conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER") result = execute(conn, """ CREATE TEMPORARY TABLE libpqjl_test ( no_nulls varchar(10) PRIMARY KEY, yes_nulls varchar(10) ); """) LibPQ.load!( (no_nulls = ["foo", "baz"], yes_nulls = ["bar", missing]), conn, "INSERT INTO libpqjl_test (no_nulls, yes_nulls) VALUES (\"1\", \"2\");", ) close(conn) ``` -------------------------------- ### Get Number of Columns in Prepared Statement Result Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Use `num_columns` to determine the number of columns that will be returned by a prepared statement's execution. This can be helpful for pre-allocating result structures. ```julia function num_columns(stmt::Statement) -> Int ``` -------------------------------- ### Customize PostgreSQL Connection Options Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md Shows how to customize PostgreSQL connection options by defining a dictionary with desired settings such as DateStyle, TimeZone, and statement_timeout. ```julia using LibPQ options = Dict( "DateStyle" => "ISO,MDY", "TimeZone" => "America/New_York", "statement_timeout" => "30000", # 30 seconds ) conn = LibPQ.Connection( "postgresql://localhost/mydb"; options=options ) ``` -------------------------------- ### Configure Type Mappings in LibPQ.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/types.md Demonstrates how to set type mappings at different levels: connection, result, and column-specific. Column-specific overrides take precedence, followed by result-level, then connection-level, and finally global defaults. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Set connection-level type map conn.type_map[LibPQ.oid(:int4)] = Int32 # Override with Result-level type_map type_map = LibPQ.PQTypeMap() type_map[LibPQ.oid(:int4)] = Int64 # Override with column-specific types column_types = LibPQ.ColumnTypeMap() column_types[1] = Int16 # First column as Int16 result = LibPQ.execute( conn, "SELECT id FROM users", type_map=type_map, column_types=column_types ) # First column is Int16 (column-specific wins) # Other int4 columns are Int64 (Result type_map wins) # Non-int4 columns use connection-level or global defaults ``` -------------------------------- ### Configure Statement Timeout Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/configuration.md Limit the execution time of SQL queries on a connection using the `statement_timeout` PostgreSQL option. This example sets a 30-second timeout and demonstrates catching the resulting `QueryCanceledError`. ```julia using LibPQ # Set 30 second statement timeout options = Dict("statement_timeout" => "30000") # milliseconds conn = LibPQ.Connection( "postgresql://localhost/mydb"; options=options ) # All queries on this connection will timeout after 30 seconds try result = LibPQ.execute(conn, "SELECT * FROM huge_table") catch err::LibPQ.Errors.QueryCanceledError println("Query exceeded 30 second timeout") end ``` -------------------------------- ### Bulk Load Data with COPY in LibPQ.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md For efficient bulk data loading, use the COPY command. This method is significantly faster than executing individual INSERT statements for large datasets. ```julia LibPQ.execute(conn, "BEGIN;") copy_in = LibPQ.CopyIn( "COPY users (name, email) FROM STDIN", [csv_data] ) LibPQ.execute(conn, copy_in) LibPQ.execute(conn, "COMMIT;") ``` -------------------------------- ### Check PostgreSQL Server Version Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md Connect to a PostgreSQL database and check the server version using LibPQ.jl. This snippet demonstrates how to establish a connection and compare the server version against a specific requirement. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") version = LibPQ.server_version(conn) if version >= LibPQ.@pqv_str("10.0") println("PostgreSQL 10 or later") end ``` -------------------------------- ### Handle PostgreSQL Result Errors Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/exceptions.md Catch and inspect PQResultError exceptions, which wrap PostgreSQL-specific errors. This example shows how to check the error code to identify a unique constraint violation and print a custom message. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") try LibPQ.execute(conn, "INSERT INTO users (id) VALUES (1), (1)") # Duplicate catch err::LibPQ.Errors.PQResultError class = LibPQ.Errors.error_class(err) code = LibPQ.Errors.error_code(err) if code == LibPQ.Errors.E23505 # Unique violation println("Duplicate entry: $err") else println("Other error: $err") end end ``` -------------------------------- ### Prepare SQL Statement Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Creates a prepared statement on the PostgreSQL server. Use this for SQL queries with parameters that will be executed multiple times. ```julia function prepare(jl_conn::Connection, query::AbstractString) -> Statement end ``` -------------------------------- ### Handle LibPQ.jl Result Errors Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/exceptions.md Catch and inspect JLResultError exceptions, which occur during LibPQ.jl's internal result handling. This example demonstrates accessing the error message when an invalid column index is used. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT 1") try value = result[1, 100] # Column out of bounds catch err::LibPQ.Errors.JLResultError println("Result error: $(err.msg)") end ``` -------------------------------- ### Customize PostgreSQL to Julia Type Mappings in LibPQ.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md Demonstrates how to customize type mappings globally, at the connection level, and at the result level in LibPQ.jl. This allows for precise control over how PostgreSQL data types are converted to Julia types. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Global customization LibPQ.LIBPQ_TYPE_MAP[LibPQ.oid(:int4)] = Int32 # Connection-level customization conn.type_map[LibPQ.oid(:float8)] = Float64 # Result-level customization type_map = LibPQ.PQTypeMap() type_map[LibPQ.oid(:text)] = String result = LibPQ.execute( conn, "SELECT id, value FROM data", type_map=type_map ) ``` -------------------------------- ### Get Column Number by Name Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Finds the 1-based index of a column within a prepared statement given its name. Returns 0 if the column name is not found. Useful for accessing specific columns by their index. ```julia function column_number(stmt::Statement, column_name::AbstractString) -> Int end ``` -------------------------------- ### LibPQ Query Execution Status Check Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/utilities.md Illustrates how to retrieve and check the status of a query execution result using LibPQ. It covers checking for successful tuple retrieval and command completion. ```julia using LibPQ # Get result status status = LibPQ.status(result) # Check status if status == LibPQ.libpq_c.PGRES_TUPLES_OK println("Query returned rows") elseif status == LibPQ.libpq_c.PGRES_COMMAND_OK println("Command completed") end ``` -------------------------------- ### Perform Vector Operations on Columns Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/tables.md Illustrates how to treat columns from a LibPQ.jl result as AbstractVectors for performing standard vector operations. This example shows collecting a column into a vector and calculating its mean, maximum, and minimum values. ```julia using LibPQ, Tables, Statistics conn = LibPQ.Connection("postgresql://localhost/mydb") result = LibPQ.execute(conn, "SELECT id, age FROM users") columns = Tables.columns(result) age_col = columns[:age] # Vector operations ages = collect(age_col) mean_age = mean(ages) max_age = maximum(ages) min_age = minimum(ages) println("Mean age: $mean_age") println("Age range: $min_age - $max_age") ``` -------------------------------- ### Execute COPY FROM STDIN with LibPQ.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/queries.md Use LibPQ.execute with a CopyIn object to perform bulk data loading into PostgreSQL. Ensure data is formatted correctly for COPY FROM STDIN. Wrap COPY operations in a transaction for optimal performance. ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Bulk load data data_lines = ["1\tAlice\t alice@example.com\n", "2\tBob\tbob@example.com\n", "3\tCharlie\tcharlie@example.com\n"] LibPQ.execute(conn, "BEGIN;") copy_in = LibPQ.CopyIn( "COPY users (id, name, email) FROM STDIN", data_lines ) result = LibPQ.execute(conn, copy_in) println("Inserted $(LibPQ.num_affected_rows(result)) rows") LibPQ.execute(conn, "COMMIT;") ``` -------------------------------- ### Convert PostgreSQL Type Name to Oid Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/types.md Use this function to get the Oid for a given PostgreSQL type name (symbol or string) or an integer Oid. It's useful when you need to specify types for database operations. ```julia using LibPQ # Get Oid for type name oid_int4 = LibPQ.oid(:int4) # 23 oid_text = LibPQ.oid("text") # 25 oid_bool = LibPQ.oid(:bool) # 16 # Convert integer directly to Oid oid = LibPQ.oid(25) ``` -------------------------------- ### LibPQ Statement Operations Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/pages/api.md Details on working with prepared statements, including retrieving metadata and loading data. ```julia LibPQ.Statement ``` ```julia num_columns(::LibPQ.Statement) ``` ```julia num_params(::LibPQ.Statement) ``` ```julia Base.show(::IO, ::LibPQ.Statement) ``` ```julia LibPQ.load! ``` -------------------------------- ### Handle ConninfoParseError Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/exceptions.md Demonstrates how to catch a ConninfoParseError when attempting to establish a connection with an invalid connection string. ```julia using LibPQ try conn = LibPQ.Connection("invalid://connection-string") catch err::LibPQ.Errors.ConninfoParseError println("Invalid connection string: $(err.msg)") end ``` -------------------------------- ### Convert Julia Array to PostgreSQL String for Execution Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/pages/type-conversions.md Demonstrates converting a Julia array to a PostgreSQL-compatible string representation for use in a query. This is necessary for types like arrays where the default Julia string format is not directly usable by PostgreSQL. The example uses `execute` with a parameterized query. ```julia A = collect(12:15); nt = columntable(execute(conn, "SELECT \"1 = ANY(\" \"2)" AS result", Any[13, string("{", join(A, ","), "}")])); nt[:result][1] ``` -------------------------------- ### prepare Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/statements.md Creates a prepared statement on the PostgreSQL server. This allows for efficient execution of the same query multiple times with different parameters. ```APIDOC ## prepare(jl_conn::Connection, query::AbstractString) ### Description Create a prepared statement on the PostgreSQL server. ### Method `prepare` ### Parameters #### Path Parameters - `jl_conn` (Connection) - Required - The database connection - `query` (AbstractString) - Required - SQL query with optional parameters (e.g., `$1`, `$2`) ### Returns `Statement` — A prepared statement object that can be executed multiple times. ### Throws - `PQResultError` — If statement preparation fails on the server - `JLConnectionError` — If connection error occurs during preparation ### Example ```julia using LibPQ conn = LibPQ.Connection("postgresql://localhost/mydb") # Prepare a statement with parameters stmt = LibPQ.prepare(conn, "SELECT * FROM users WHERE id = \$1 AND active = \$2") # Execute multiple times with different parameters result1 = LibPQ.execute(stmt, [1, true]) result2 = LibPQ.execute(stmt, [2, false]) ``` ``` -------------------------------- ### Connect to Amazon Redshift with LibPQ.jl Source: https://github.com/juliadatabases/libpq.jl/blob/master/docs/src/pages/faq.md Override default client options by passing an empty Dict{String, String} to the options parameter when creating a LibPQ.Connection to ensure compatibility with Amazon Redshift. ```julia conn = LibPQ.Connection("dbname=myredshift"; options=Dict{String, String}()) ``` -------------------------------- ### Basic PostgreSQL Connection Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/api-reference/connections.md Establishes a basic connection to a PostgreSQL database using a standard connection string format. ```julia conn = LibPQ.Connection("postgresql://user:password@localhost/dbname") ``` -------------------------------- ### Using DBInterface.jl for Database Operations Source: https://github.com/juliadatabases/libpq.jl/blob/master/_autodocs/library-overview.md Connect to a database, prepare a statement, and execute it using the DBInterface.jl package. This provides a standardized way to interact with various databases. ```julia using LibPQ, DBInterface db = DBInterface.connect(LibPQ.Connection, "postgresql://localhost/mydb") stmt = DBInterface.prepare(db, "SELECT * FROM users WHERE id = \"1\") result = DBInterface.execute(stmt, [42]) ```