How To Run Postgres locally

flask, heroku, postgresql, python

Solution

First step, is to get Flask + Postgresql running locally, and the first step to do that is to install postgresql on your machine. Next, you should install the python drivers for postgresql.

For Windows, you can use the windows installer for postgresql and the windows installer for the python drivers.

Once you have finished the above two steps, you need to create a database.

For Windows, you can use the bundled pgadminIII tool. Here is a video that shows how to do just that.

For example, here is how you create a database called `the_database`, and create a user called `databaseuser` with a password `P@ssw0rd` using the built-in tool `psql`:

$ psql -d template1 -U postgres
template1=# CREATE USER databaseuser WITH PASSWORD 'P@ssw0rd';
template1=# CREATE DATABASE the_database;
template1=# GRANT ALL PRIVILEGES ON DATABASE the_database to databaseuser;
template1=# \q

Here is how you would configure your application with the above information:

db_conn = 'postgresql+psycopg2://databaseuser:P@ssw0rd@localhost/the_database' 
app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = db_conn
db = SQLAlchemy(app)

Problem

I read the Postgres docs for Flask and they said that to run Postgres you should have the following code ``` app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = postgresql://localhost/[YOUR_DB_NAME]' db = SQLAlchemy(app) ``` How do I know my database name? I wrote db as the name - but I got an error ``` sqlalchemy.exc.OperationalError: (OperationalError) FATAL: database "[db]" does not exist ``` Running Heroku with Flask if that helps

Original source