title: How to setup postgres database and tables in node pg
body: <p>I want to share a <code>Node</code> repo, and ideally it should be ready to use after the usual <code>yarn install &amp;&amp; yarn start</code>.</p>
  <p>However <a href="https://node-postgres.com/" rel="noreferrer"><code>pg</code></a> can't connect neither <code>SELECT</code> to/from a database and table that don't exist yet.</p>
  <p>It's a hassle but at the moment these previous steps are required:</p>
  <ol>
  <li>Get postgres; <code>docker run --name my-postgres -e POSTGRES_PASSWORD={MY_PASSWORD} -p 5432:5432 -d --rm postgres:13.0</code></li>
  <li>Lauch psql; <code>docker exec -it -u postgres my-postgres psql</code></li>
  <li><code>CREATE DATABASE MY_DB</code></li>
  <li><code>\connect MY_DB</code></li>
  <li>paste the following SQL script to create tables</li>
  </ol>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE my_table (
    id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    field_1 VARCHAR ( 50 ) UNIQUE NOT NULL,
    field_2 INT NOT NULL,
  );
  </code></pre>
  <p>Since I am already getting postgres from docker, is <code>docker-componse</code> the <em>logical</em> solution?
  I'm not too familiar with <code>dockerfile</code>, is there an alternative?</p>
  <p>What are the best practises to initialize postgres database in node?</p>
  <p>How bad an idea would it be calling a script on <code>yarn start</code> (or <code>yarn setup</code>). Any example of such a script would be appreciated.</p>