title: How to connect to the database using .env inside typeorm
body: <p>I tried to connect the database using <code>ormconfig.js</code> and create <code>.env</code> but I get an error:</p>
  <blockquote>
  <p>Error: connect ECONNREFUSED 127.0.0.1:3000
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js: 1141: 16) ORMCONFIG.JS</p>
  <p>errno: 'ECONNREFUSED',   code: 'ECONNREFUSED',   syscall: 'connect',
  address: '127.0.0.1',   port: 3000,   fatal: true }</p>
  </blockquote>
  <pre><code>module.exports = {
  &quot;type&quot;: process.env.TYPEORM_CONNECTION,
  &quot;url&quot; : process.env.DATABASE_URL,
  &quot;host&quot;: process.env.TYPEORM_HOST,
  &quot;port&quot;: process.env.TYPEORM_PORT,
  &quot;username&quot;: process.env.TYPEORM_USERNAME,
  &quot;password&quot;: process.env.TYPEORM_PASSWORD,
  &quot;database&quot;: process.env.TYPEORM_DATABASE,
  &quot;synchronize&quot;: process.env.TYPEORM_SYNCHRONIZE,
  &quot;logging&quot;: process.env.TYPEORM_LOGGING,
  &quot;entities&quot;: [
     &quot;dist/entity/**/*.ts&quot;
  ],
  &quot;migrations&quot;: [
     &quot;dist/migration/**/*.ts&quot;
  ],
  &quot;subscribers&quot;: [
     &quot;dist/subscriber/**/*.ts&quot;
  ],
  &quot;cli&quot;: {
     &quot;entitiesDir&quot;: &quot;src/entity&quot;,
     &quot;migrationsDir&quot;: &quot;src/migration&quot;,
     &quot;subscribersDir&quot;: &quot;src/subscriber&quot;
  }}
  </code></pre>
  <p>.env</p>
  <pre><code>DATABASE_URL= http://root:@localhost:3000
  TYPEORM_CONNECTION = mysql
  TYPEORM_HOST = localhost
  TYPEORM_USERNAME = root
  TYPEORM_PASSWORD = 
  TYPEORM_DATABASE = login_controlaccess
  TYPEORM_PORT = 3000
  TYPEORM_SYNCHRONIZE = true
  TYPEORM_LOGGING = true
  </code></pre>
  <p>index.js</p>
  <pre><code>import &quot;reflect-metadata&quot;;
  import * as dotenv from 'dotenv';
  import {createConnection} from &quot;typeorm&quot;;
  import * as express from &quot;express&quot;;
  import {Request, Response} from &quot;express&quot;;
  import * as cors from 'cors';
  import * as helmet from  'helmet';
  import routes from './routers';
  dotenv.config();
  const PORT= process.env.TYPEORM_PORT || 3000;
  
  
  createConnection().then(async () =&gt; {
  
      // create express app
         const app = express();
      //middleware
        app.use(cors());
        app.use(helmet());
        app.use(express.json());
       //ROuter
        app.use('/', routes); 
      // start express server
      app.listen(PORT, ()=&gt; console.log(`server running in port ${PORT}`));
  
  
      })
      .catch(error =&gt; console.log(error));
  </code></pre>