title: How do I use a custom Tokio runtime within tokio_postgres (and without the tokio::main macro)?
body: <p>How would I implement this tokio_postgres example with a custom tokio runtime builder and without the main macro?</p>
  <p>This works fine, per the tokio_postgres <a href="https://docs.rs/tokio-postgres/0.6.0/tokio_postgres/index.html" rel="nofollow noreferrer">docs</a>:</p>
  <p>examples/withmacro.rs</p>
  <pre><code>use tokio_postgres::{NoTls, Error};
  
  async fn db_main()-&gt; Result&lt;(), Error&gt; {
      
      // pasted from: https://docs.rs/tokio-postgres/0.6.0/tokio_postgres/index.html
      
      // Connect to the database.
      let conn_string = std::env::var(&quot;PG_CONNECT&quot;).unwrap();
      let (client, connection) = tokio_postgres::connect(&amp;conn_string,NoTls).await?;
      
      // The connection object performs the actual communication
      tokio::spawn(async move {
          if let Err(e) = connection.await {
              eprintln!(&quot;connection error: {}&quot;, e);
          }
      });
  
      // Now we can execute a simple statement that just returns its parameter.
      let rows = client
          .query(&quot;SELECT $1::TEXT&quot;, &amp;[&amp;&quot;hello world&quot;])
          .await?;
  
      // And then check that we got back the same string we sent over.
      let value: &amp;str = rows[0].get(0);
      println!(&quot;value: {:?}&quot;, &amp;value);
      assert_eq!(value, &quot;hello world&quot;);
  
      Ok(())
  }
  
  #[tokio::main]
  async fn main() {
      dotenv::dotenv().ok();
      let _ = db_main().await;
  }
  </code></pre>
  <p>However, I'd like to customize the tokio runtime builder as in the main below--and not use the tokio macro. This, however, panics saying &quot;there is no reactor running&quot; when it comes to the &quot;tokio_postgres::connect&quot; at runtime. How should I reconfigure the following code to use my own Tokio runtime instance within tokio_postgres (assuming that's even what I need)?</p>
  <p>examples/nomacro.rs</p>
  <pre><code>use tokio_postgres::{NoTls, Error};
  
  async fn db_main()-&gt; Result&lt;(), Error&gt; {
      // Connect to the database.
      let conn_string = std::env::var(&quot;PG_CONNECT&quot;).unwrap();
      
      // This line panics with: 
      // &quot;thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime', /Users/me/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/io/driver/mod.rs:254:13&quot;
      let (client, connection) = tokio_postgres::connect(&amp;conn_string, NoTls).await?;
      
      // ...
      Ok(())
  }
      
  fn main() {
      dotenv::dotenv().ok();
  
      // Tokio 0.3
      let rt:Runtime = tokio::runtime::Builder::new_multi_thread()
          .worker_threads(6)
          .thread_name(&quot;my thread&quot;)
          .build()
          .unwrap();
  
      rt.block_on( async {
          db_main().await;
      });
  }
  </code></pre>
  <p>Should I be passing a reference to the runtime to <code>db_main()</code> to give to an instance of tokio_postgres <code>Config</code>? I've experimented with disabling the tokio instance in tokio_postgres with <code>&quot;tokio-postgres = { version = &quot;0.6.0&quot;, default-features = false}&quot;</code> in Cargo.toml.</p>
  <p>Baseline Cargo.toml:</p>
  <pre><code>[dependencies]
  dotenv = &quot;0.15.0&quot;
  tokio = { version = &quot;0.3&quot;, features = [&quot;rt-multi-thread&quot;, &quot;macros&quot;] }
  tokio-postgres = { version = &quot;0.6.0&quot;}
  # tokio-postgres = { version = &quot;0.6.0&quot;, default-features = false}
  </code></pre>