How To Run Postgres locally

30,557

Solution 1

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)

Solution 2

I do not have experience with Heroku, but from your messages I would say you don't have PostgreSQL running and you dont have a database at all. Looks like here they have some info https://postgres.heroku.com/ :)

But in your topic you asking about localhost PostgreSQL... So if you want to run your setup locally, first thing to check is if you have PostgreSQL installed and running. If you do, try to connect to it with pgAdmin or from console and create database. If you can't connect - check config for ports, hosts PostgreSQL is listening.

Anyway those messages I see you posted in question in comments, gives me only the idea that you don't have DB created or even running (or you have wrong config).

Share:
30,557
Rohit Rayudu
Author by

Rohit Rayudu

Updated on July 05, 2022

Comments

  • Rohit Rayudu
    Rohit Rayudu almost 2 years

    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