0
votes

I want to connect to the Postgresql database using Python in Windows. This is the function I have written:

def config(filename='', section='postgresql'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
# get section, default to postgresql
db = {}
if parser.has_section(section):
    params = parser.items(section)
    for param in params:
        db[param[0]] = param[1]
else:
    raise Exception('section {0} not found in the {1} file'.format(section, filename))
return db

I have read that I should put "database.ini" in front of "filename". However, it gives me the following message:

section postgresql not found in the database.ini file

Where should I find the "database.ini" in Windows?

1
This makes no sense. What library are you using to try to connect? Where is database.ini coming from? I see nothing in this code that will result in a connection. What is the purpose of this code?Adrian Klaver

1 Answers

1
votes

I'm using the same code and had the same issue. I was able to resolve the issue by moving all of my code up one folder level.

I am working in vscode on Windows 10. My python source code was located in a nested folder like c:\repos\myProject\myProject. I moved everything up to c:\repos\myProject and then it worked fine.

I can't explain why that has solved the problem but it has in my case. Perhaps it's related to the location of the virtual env or pycache

Also to answer your question about database.ini - you create that file yourself and can put it in the same relative path. Useful resource here

Anyway I can confirm your code definitely works on Windows 10.