import os
import sys

import psycopg2
import sqlalchemy as sa
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


def main():
    database = 'postgres'
    user = 'postgres'

    url = sa.engine.url.URL('postgresql', host=os.environ['PGHOST'],
                            database=database, username=user)
    ddl_text = sa.text('CREATE DATABASE {};'.format(os.environ['PGDATABASE']))
    engine = sa.create_engine(url)
    engine.raw_connection().set_isolation_level(
        ISOLATION_LEVEL_AUTOCOMMIT
    )

    try:
        engine.execute(ddl_text)
        sys.stdout.write('Creating environment successfully.\n')
    except psycopg2.Error:
        raise SystemExit('Could not connect to PostgreSQL.\n{0}'.format(sys.exc_info()))


if __name__ == '__main__':
    main()

import os
import sys

import psycopg2
import sqlalchemy as sa
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


def main():
    database = 'postgres'
    user = 'postgres'

    url = sa.engine.url.URL(
        'postgresql', host=os.environ['PGHOST'], database=database,
        username=user, password=os.environ.get('PGPASSWORD', None)
    )
    ddl_text = sa.DDL('CREATE DATABASE {};'.format(os.environ['PGDATABASE']))
    engine = sa.create_engine(url)
    engine.raw_connection().set_isolation_level(
        ISOLATION_LEVEL_AUTOCOMMIT
    )

    try:
        engine.execute(ddl_text)
        sys.stdout.write('Creating environment successfully.\n')
    except psycopg2.Error:
        raise SystemExit('Could not connect to PostgreSQL.\n{0}'.format(sys.exc_info()))


if __name__ == '__main__':
    main()

Add password envar for db creation.
Add password envar for db creation.

  import os
  import sys
  
  import psycopg2
  import sqlalchemy as sa
  from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
  
  
  def main():
      database = 'postgres'
      user = 'postgres'
  
-     url = sa.engine.url.URL('postgresql', host=os.environ['PGHOST'],
-                             database=database, username=user)
+     url = sa.engine.url.URL(
+         'postgresql', host=os.environ['PGHOST'], database=database,
+         username=user, password=os.environ.get('PGPASSWORD', None)
+     )
-     ddl_text = sa.text('CREATE DATABASE {};'.format(os.environ['PGDATABASE']))
+     ddl_text = sa.DDL('CREATE DATABASE {};'.format(os.environ['PGDATABASE']))
      engine = sa.create_engine(url)
      engine.raw_connection().set_isolation_level(
          ISOLATION_LEVEL_AUTOCOMMIT
      )
  
      try:
          engine.execute(ddl_text)
          sys.stdout.write('Creating environment successfully.\n')
      except psycopg2.Error:
          raise SystemExit('Could not connect to PostgreSQL.\n{0}'.format(sys.exc_info()))
  
  
  if __name__ == '__main__':
      main()
  
Add password envar for db creation.
## Code Before:
import os
import sys

import psycopg2
import sqlalchemy as sa
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


def main():
    database = 'postgres'
    user = 'postgres'

    url = sa.engine.url.URL('postgresql', host=os.environ['PGHOST'],
                            database=database, username=user)
    ddl_text = sa.text('CREATE DATABASE {};'.format(os.environ['PGDATABASE']))
    engine = sa.create_engine(url)
    engine.raw_connection().set_isolation_level(
        ISOLATION_LEVEL_AUTOCOMMIT
    )

    try:
        engine.execute(ddl_text)
        sys.stdout.write('Creating environment successfully.\n')
    except psycopg2.Error:
        raise SystemExit('Could not connect to PostgreSQL.\n{0}'.format(sys.exc_info()))


if __name__ == '__main__':
    main()

## Instruction:
Add password envar for db creation.
## Code After:
import os
import sys

import psycopg2
import sqlalchemy as sa
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


def main():
    database = 'postgres'
    user = 'postgres'

    url = sa.engine.url.URL(
        'postgresql', host=os.environ['PGHOST'], database=database,
        username=user, password=os.environ.get('PGPASSWORD', None)
    )
    ddl_text = sa.DDL('CREATE DATABASE {};'.format(os.environ['PGDATABASE']))
    engine = sa.create_engine(url)
    engine.raw_connection().set_isolation_level(
        ISOLATION_LEVEL_AUTOCOMMIT
    )

    try:
        engine.execute(ddl_text)
        sys.stdout.write('Creating environment successfully.\n')
    except psycopg2.Error:
        raise SystemExit('Could not connect to PostgreSQL.\n{0}'.format(sys.exc_info()))


if __name__ == '__main__':
    main()
