SelectoMix.Introspector.Postgres (selecto_mix v0.4.2)
Introspects PostgreSQL databases directly using system catalogs.
Works with Postgrex connections without requiring Ecto schemas. Uses information_schema and pg_catalog to discover table structure, relationships, and constraints.
Usage
{:ok, conn} = Postgrex.start_link(hostname: "localhost", database: "mydb")
# List all tables
{:ok, tables} = SelectoMix.Introspector.Postgres.list_tables(conn)
# Introspect specific table
{:ok, metadata} = SelectoMix.Introspector.Postgres.introspect_table(conn, "users")
Summary
Functions
Get column definitions for a table.
Get enum values for a PostgreSQL enum type.
Get foreign key relationships for a table.
Get index definitions for a table.
Get primary key column(s) for a table.
Introspect a table and return complete metadata.
List all tables in a schema.
Map PostgreSQL type to Elixir/Ecto type.
Functions
Get column definitions for a table.
Returns detailed column information from information_schema.columns.
Returns
List of column maps with keys:
:column_name- Column name (atom):data_type- PostgreSQL type name:udt_name- User-defined type name (for enums, etc):is_nullable- "YES" or "NO":column_default- Default value expression:character_maximum_length- For string types:numeric_precision- For numeric types:numeric_scale- For numeric types
Get enum values for a PostgreSQL enum type.
Parameters
conn- Postgrex connectionenum_type_name- Name of the enum type (string)
Returns
{:ok, [value1, value2, ...]}- List of enum values as strings{:error, reason}- Query error or type not found
Get foreign key relationships for a table.
Returns information about foreign key constraints, which can be used to infer associations (belongs_to relationships).
Returns
{:ok, [foreign_key_info, ...]}- List of foreign key maps{:error, reason}- Query error
Each foreign key map contains:
:constraint_name- Name of the constraint:column_name- Column in this table (atom):foreign_table_schema- Referenced table schema:foreign_table_name- Referenced table name:foreign_column_name- Referenced column name (atom)
Get index definitions for a table.
Returns
{:ok, [index_info, ...]}- List of index maps{:error, reason}- Query error
Get primary key column(s) for a table.
Returns the primary key field name, or a list of field names for composite keys.
Returns
{:ok, :id}- Single primary key field{:ok, [:field1, :field2]}- Composite primary key{:ok, nil}- No primary key defined{:error, reason}- Query error
Introspect a table and return complete metadata.
Returns standardized metadata structure compatible with Ecto introspection format used by SelectoMix.
Parameters
conn- Postgrex connectiontable_name- Table nameopts- Options:schema- Schema name (default: "public"):include_indexes- Include index information (default: false)
Returns
{:ok, metadata}- Table metadata map{:error, reason}- Error details
The metadata map includes:
:table_name- Table name:schema- Schema name:fields- List of field names:field_types- Map of field name to Elixir type:primary_key- Primary key field name (or list for composite keys):associations- Map of detected foreign key relationships:columns- Detailed column metadata
List all tables in a schema.
Parameters
conn- Postgrex connection (PID or named process)schema- Schema name (default: "public")
Returns
{:ok, [table_name, ...]}- List of table names{:error, reason}- Error details
Examples
{:ok, tables} = list_tables(conn)
{:ok, tables} = list_tables(conn, "inventory")
Map PostgreSQL type to Elixir/Ecto type.
Converts PostgreSQL type names to the corresponding Elixir type atoms used by Ecto and Selecto.
Parameters
data_type- PostgreSQL data type name from information_schemaudt_name- User-defined type name (for enums and custom types)conn- Optional Postgrex connection for enum detection
Returns
Elixir type atom (:integer, :string, :boolean, etc)