@@ -644,6 +644,50 @@ impl NodeBuilder {
644644 self . build_with_store_and_logger ( node_entropy, kv_store, logger)
645645 }
646646
647+ /// Builds a [`Node`] instance with a [PostgreSQL] backend and according to the options
648+ /// previously configured.
649+ ///
650+ /// Connects to the PostgreSQL database at the given `connection_string`, e.g.,
651+ /// `"postgres://user:password@localhost/ldk_db"`.
652+ ///
653+ /// The given `db_name` will be used or default to
654+ /// [`DEFAULT_DB_NAME`](io::postgres_store::DEFAULT_DB_NAME). The `connection_string` must
655+ /// not include a `dbname` when `db_name` is set, providing both is an error. The database
656+ /// will be created automatically if it doesn't already exist. The initial connection is
657+ /// made to the target database, and if it fails we fall back to the default `postgres`
658+ /// database to create it.
659+ ///
660+ /// The given `kv_table_name` will be used or default to
661+ /// [`DEFAULT_KV_TABLE_NAME`](io::postgres_store::DEFAULT_KV_TABLE_NAME).
662+ ///
663+ /// If `certificate_pem` is `Some`, TLS will be used for database connections and the
664+ /// provided PEM-encoded CA certificate will be added to the system's default root
665+ /// certificates (it does not replace them). If `certificate_pem` is `None`, connections
666+ /// will be unencrypted.
667+ ///
668+ /// [PostgreSQL]: https://www.postgresql.org
669+ #[ cfg( feature = "postgres" ) ]
670+ pub fn build_with_postgres_store (
671+ & self , node_entropy : NodeEntropy , connection_string : String , db_name : Option < String > ,
672+ kv_table_name : Option < String > , certificate_pem : Option < String > ,
673+ ) -> Result < Node , BuildError > {
674+ let logger = setup_logger ( & self . log_writer_config , & self . config ) ?;
675+ let runtime = self . setup_runtime ( & logger) ?;
676+ let kv_store = runtime
677+ . block_on ( io:: postgres_store:: PostgresStore :: new_with_logger (
678+ connection_string,
679+ db_name,
680+ kv_table_name,
681+ certificate_pem,
682+ Some ( Arc :: clone ( & logger) ) ,
683+ ) )
684+ . map_err ( |e| {
685+ log_error ! ( logger, "Failed to set up Postgres store: {e}" ) ;
686+ BuildError :: KVStoreSetupFailed
687+ } ) ?;
688+ self . build_with_store_runtime_and_logger ( node_entropy, kv_store, runtime, logger)
689+ }
690+
647691 /// Builds a [`Node`] instance with a [`FilesystemStoreV2`] backend and according to the options
648692 /// previously configured.
649693 ///
@@ -789,18 +833,27 @@ impl NodeBuilder {
789833 self . build_with_store_and_logger ( node_entropy, kv_store, logger)
790834 }
791835
792- fn build_with_store_and_logger < S : SyncAndAsyncKVStore + Send + Sync + ' static > (
793- & self , node_entropy : NodeEntropy , kv_store : S , logger : Arc < Logger > ,
794- ) -> Result < Node , BuildError > {
795- let runtime = if let Some ( handle) = self . runtime_handle . as_ref ( ) {
796- Arc :: new ( Runtime :: with_handle ( handle. clone ( ) , Arc :: clone ( & logger) ) )
836+ fn setup_runtime ( & self , logger : & Arc < Logger > ) -> Result < Arc < Runtime > , BuildError > {
837+ if let Some ( handle) = self . runtime_handle . as_ref ( ) {
838+ Ok ( Arc :: new ( Runtime :: with_handle ( handle. clone ( ) , Arc :: clone ( logger) ) ) )
797839 } else {
798- Arc :: new ( Runtime :: new ( Arc :: clone ( & logger) ) . map_err ( |e| {
840+ Ok ( Arc :: new ( Runtime :: new ( Arc :: clone ( logger) ) . map_err ( |e| {
799841 log_error ! ( logger, "Failed to setup tokio runtime: {}" , e) ;
800842 BuildError :: RuntimeSetupFailed
801- } ) ?)
802- } ;
843+ } ) ?) )
844+ }
845+ }
846+
847+ fn build_with_store_and_logger < S : SyncAndAsyncKVStore + Send + Sync + ' static > (
848+ & self , node_entropy : NodeEntropy , kv_store : S , logger : Arc < Logger > ,
849+ ) -> Result < Node , BuildError > {
850+ let runtime = self . setup_runtime ( & logger) ?;
851+ self . build_with_store_runtime_and_logger ( node_entropy, kv_store, runtime, logger)
852+ }
803853
854+ fn build_with_store_runtime_and_logger < S : SyncAndAsyncKVStore + Send + Sync + ' static > (
855+ & self , node_entropy : NodeEntropy , kv_store : S , runtime : Arc < Runtime > , logger : Arc < Logger > ,
856+ ) -> Result < Node , BuildError > {
804857 let seed_bytes = node_entropy. to_seed_bytes ( ) ;
805858 let config = Arc :: new ( self . config . clone ( ) ) ;
806859
@@ -1116,6 +1169,58 @@ impl ArcedNodeBuilder {
11161169 self . inner . read ( ) . expect ( "lock" ) . build ( * node_entropy) . map ( Arc :: new)
11171170 }
11181171
1172+ /// Builds a [`Node`] instance with a [PostgreSQL] backend and according to the options
1173+ /// previously configured.
1174+ ///
1175+ /// Connects to the PostgreSQL database at the given `connection_string`, e.g.,
1176+ /// `"postgres://user:password@localhost/ldk_db"`.
1177+ ///
1178+ /// The given `db_name` will be used or default to
1179+ /// [`DEFAULT_DB_NAME`](io::postgres_store::DEFAULT_DB_NAME). The `connection_string` must
1180+ /// not include a `dbname` when `db_name` is set, providing both is an error. The database
1181+ /// will be created automatically if it doesn't already exist. The initial connection is
1182+ /// made to the target database, and if it fails we fall back to the default `postgres`
1183+ /// database to create it.
1184+ ///
1185+ /// The given `kv_table_name` will be used or default to
1186+ /// [`DEFAULT_KV_TABLE_NAME`](io::postgres_store::DEFAULT_KV_TABLE_NAME).
1187+ ///
1188+ /// If `certificate_pem` is `Some`, TLS will be used for database connections and the
1189+ /// provided PEM-encoded CA certificate will be added to the system's default root
1190+ /// certificates (it does not replace them). If `certificate_pem` is `None`, connections
1191+ /// will be unencrypted.
1192+ ///
1193+ /// [PostgreSQL]: https://www.postgresql.org
1194+ #[ cfg( feature = "postgres" ) ]
1195+ pub fn build_with_postgres_store (
1196+ & self , node_entropy : Arc < NodeEntropy > , connection_string : String , db_name : Option < String > ,
1197+ kv_table_name : Option < String > , certificate_pem : Option < String > ,
1198+ ) -> Result < Arc < Node > , BuildError > {
1199+ self . inner
1200+ . read ( )
1201+ . unwrap ( )
1202+ . build_with_postgres_store (
1203+ * node_entropy,
1204+ connection_string,
1205+ db_name,
1206+ kv_table_name,
1207+ certificate_pem,
1208+ )
1209+ . map ( Arc :: new)
1210+ }
1211+
1212+ /// Builds a [`Node`] instance with a [PostgreSQL] backend and according to the options
1213+ /// previously configured.
1214+ ///
1215+ /// This requires the `postgres` crate feature.
1216+ #[ cfg( not( feature = "postgres" ) ) ]
1217+ pub fn build_with_postgres_store (
1218+ & self , _node_entropy : Arc < NodeEntropy > , _connection_string : String ,
1219+ _db_name : Option < String > , _kv_table_name : Option < String > , _certificate_pem : Option < String > ,
1220+ ) -> Result < Arc < Node > , BuildError > {
1221+ Err ( BuildError :: KVStoreSetupFailed )
1222+ }
1223+
11191224 /// Builds a [`Node`] instance with a [`FilesystemStoreV2`] backend and according to the options
11201225 /// previously configured.
11211226 pub fn build_with_fs_store (
0 commit comments