Infura provides reliable, scalable blockchain infrastructure for Ethereum, Polygon, and other EVM-compatible networks via JSON-RPC APIs.
The Infura Wrapper is a WebAssembly (Wasm) foreign data wrapper which allows you to read blockchain data (blocks, transactions, balances, logs) directly from your Postgres database.
By default, Postgres stores FDW credentials inside pg_catalog.pg_foreign_server in plain text. Anyone with access to this table will be able to view these credentials. Wrappers is designed to work with Vault, which provides an additional level of security for storing credentials. We recommend using Vault to store your credentials.
123456
-- Save your Infura API key in Vault and retrieve the created `key_id`selectvault.create_secret('<Infura API Key>','infura','Infura API key for blockchain data access');
Connecting to Infura
We need to provide Postgres with the credentials to access Infura and any additional options. We can do this using the create server command:
1 2 3 4 5 6 7 8 910
createserverinfura_serverforeigndatawrapperwasm_wrapperoptions(fdw_package_url'https://github.com/supabase/wrappers/releases/download/wasm_infura_fdw_v0.1.0/infura_fdw.wasm',fdw_package_name'supabase:infura-fdw',fdw_package_version'0.1.0',fdw_package_checksum'6cb829b851ea8cbd70cb893958826824388a4d9477305a16f2f489bcd569b35e',api_key_id'<key_ID>',-- The Key ID from above.network'mainnet'-- optional, defaults to mainnet);
1 2 3 4 5 6 7 8 910
createserverinfura_serverforeigndatawrapperwasm_wrapperoptions(fdw_package_url'https://github.com/supabase/wrappers/releases/download/wasm_infura_fdw_v0.1.0/infura_fdw.wasm',fdw_package_name'supabase:infura-fdw',fdw_package_version'0.1.0',fdw_package_checksum'6cb829b851ea8cbd70cb893958826824388a4d9477305a16f2f489bcd569b35e',api_key'<your-infura-api-key>',network'mainnet'-- optional, defaults to mainnet);
Note the fdw_package_* options are required, which specify the Wasm package metadata. You can get the available package version list from above.
Supported Networks
The network option supports the following values:
Network
Value
Chain ID
Ethereum Mainnet
mainnet
1
Ethereum Sepolia
sepolia
11155111
Polygon PoS
polygon-mainnet
137
Polygon Amoy
polygon-amoy
80002
Arbitrum One
arbitrum-mainnet
42161
Optimism
optimism-mainnet
10
Base
base-mainnet
8453
Linea
linea-mainnet
59144
Create a schema
We recommend creating a schema to hold all the foreign tables:
1
createschemaifnotexistsinfura;
Options
The full list of foreign table options are below:
resource - Resource type to query, required. One of: blocks, transactions, balances, logs, chain_info
Entities
Below are all the entities supported by this FDW. Each entity maps to a specific JSON-RPC method on the Infura API.
We can use SQL import foreign schema to import foreign table definitions from Infura.
For example, using below SQL can automatically create foreign tables in the infura schema.
1 2 3 4 5 6 7 8 9101112
-- create all the foreign tablesimportforeignschemainfurafromserverinfura_serverintoinfura;-- or, create selected tables onlyimportforeignschemainfuralimitto("eth_blocks","eth_transactions")fromserverinfura_serverintoinfura;-- or, create all foreign tables except selected tablesimportforeignschemainfuraexcept("eth_blocks")fromserverinfura_serverintoinfura;
Blocks
Query Ethereum block data using eth_getBlockByNumber.
This section describes important limitations and considerations when using this FDW:
Read-only: Blockchain data is immutable. This FDW only supports SELECT operations.
Rate limiting: Infura API has rate limits. Consider using materialized views for frequently accessed data.
Large numeric values: Ethereum values (like Wei balances) can be extremely large. Use text type for these columns.
Block range limits: For eth_getLogs, Infura may limit the block range you can query at once.
Examples
Below are some examples on how to use Infura foreign tables.
Query latest block
1 2 3 4 5 6 7 8 910111213141516171819202122
createforeigntableinfura.eth_blocks(numbernumeric,hashtext,parent_hashtext,timestamptimestamp,minertext,gas_usednumeric,gas_limitnumeric,transaction_countbigint,base_fee_per_gasnumeric,attrsjsonb)serverinfura_serveroptions(resource'blocks');-- Query the latest blockselect*frominfura.eth_blocks;-- Query a specific blockselect*frominfura.eth_blockswherenumber=19000000;
Query account balance
1 2 3 4 5 6 7 8 910111213141516
createforeigntableinfura.eth_balances(addresstext,balancetext,blocktext)serverinfura_serveroptions(resource'balances');-- Query Vitalik's wallet balance (in ETH)selectaddress,balanceasbalance_ethfrominfura.eth_balanceswhereaddress='0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
createforeigntableinfura.eth_transactions(hashtext,block_numbernumeric,block_hashtext,from_addresstext,to_addresstext,valuenumeric,gasnumeric,gas_pricenumeric,noncenumeric,inputtext,transaction_indexnumeric,attrsjsonb)serverinfura_serveroptions(resource'transactions');-- Query a specific transactionselectfrom_address,to_address,value/1e18asvalue_ethfrominfura.eth_transactionswherehash='0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060';
Query event logs
1 2 3 4 5 6 7 8 91011121314151617181920
createforeigntableinfura.eth_logs(addresstext,block_numbernumeric,block_hashtext,transaction_hashtext,transaction_indexnumeric,log_indexnumeric,datatext,topicsjsonb,removedboolean,attrsjsonb)serverinfura_serveroptions(resource'logs');-- Query logs from a specific contractselect*frominfura.eth_logswhereaddress='0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
Query chain information
1 2 3 4 5 6 7 8 9101112131415161718
createforeigntableinfura.eth_chain_info(networktext,chain_idnumeric,block_numbernumeric,gas_pricenumeric)serverinfura_serveroptions(resource'chain_info');-- Get current chain statusselectnetwork,chain_id,block_number,gas_price/1e9asgas_price_gweifrominfura.eth_chain_info;
-- Create a separate server for Polygoncreateserverpolygon_serverforeigndatawrapperwasm_wrapperoptions(fdw_package_url'https://github.com/supabase/wrappers/releases/download/wasm_infura_fdw_v0.1.0/infura_fdw.wasm',fdw_package_name'supabase:infura-fdw',fdw_package_version'0.1.0',fdw_package_checksum'6cb829b851ea8cbd70cb893958826824388a4d9477305a16f2f489bcd569b35e',api_key'<your-infura-api-key>',network'polygon-mainnet');createforeigntableinfura.polygon_blocks(numbernumeric,hashtext,parent_hashtext,timestamptimestamp,minertext,gas_usednumeric,gas_limitnumeric,transaction_countbigint,base_fee_per_gasnumeric,attrsjsonb)serverpolygon_serveroptions(resource'blocks');select*frominfura.polygon_blocks;