Step by step to deploy a FLask App in Heroku with python3, virtualenv and PostgreSQL




  1. Criar app no Heroku

    Isso pode ser feito tanto pela CLI quanto pelo site do Heroku.

  2. criar DB

    Ver em comandos uteis abaixo como fazer.

  3. Criar repo git na rais do projeto

    git init

    git add .

    git commit -m "first commit"

    git push -u origim master

  4. Criar virtualenv
    virtualenv -p python3 myenv
  5. Login into heroku cli
    heroku login
  6. ligar o heroku ao git
    heroku git:remote -a app_name
  7. para checar se os dois repositorios estao ligados.
    git remote -v
  8. Instalar pacotes necessarios e gerar novo requirements.txt

    pip freeze > requirements.txt

    git add requirements.txt

  9. fazer dump do DB

    pg_dump -d database_name -U username -h hostname > filename.dump
  10. substituir usuario no dump para usuario do db do Heroku se precisar
    sed -i 's/old-text/new-text/g' input.txt
  11. Fazer commit das alteracoes

    git commit -m "requirements.txt e dump do DB gerados"

    git push -u origin master

    git push heroku master

    O app sera instalado ao fazer push para o repositorio heroku




Comandos uteis

create database database_name; #to create a database

drop database database_name; #to delete a database

ALTER ROLE username WITH PASSWORD 'new_password'; # alterar senha do usuario do postgresql

psql -h host -U username -d database_name -f filename.dump # carregar database com arquivo de dump

sudo -u postgres psql postgres # acessar ao db


Delete db when there is active connections

  • SELECT * FROM pg_stat_activity WHERE datname = db_name;
  • SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'db_name;

Links you might be interested!


Birds from my backyard. here

Arduino based Aquarium Automation System. here

How to deploy Flask App on Heroku. here

Flask App on Digitalocean Droplet and Nginx.here



© Copyright 2019 by marumaker.com