0

I am using the community version of pycharm, my hope was to put the sensitive database connection credentials in a separate file in my pycharm project, so if I shared my other files that contain the actual code, they wouldn't have my connection info. Here is what the "connect1.py" file contains:

import psycopg2

# Database Credentials
DB_HOST = "localhost"
DB_NAME = "movie_watchlist1"
DB_USER = "postgres"
DB_PASS = "postgres123"

def database_credentials():
    psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASS, host=DB_HOST)
    pass

Here are the lines in my database.py file that tries to access this information:

import psycopg2

from connect1 import database_credentials

connection = psycopg2.connect(database_credentials())

And here is the error:

TypeError: missing dsn and no parameters

I think the problem is with the "connection = psycopg2.connect(database_credentials())" line, but I haven't been able to figure it out, any help or suggestions would be greatly appreciated.

Dan
  • 1
  • 1
  • Does this answer your question? [Importing variables from another file?](https://stackoverflow.com/questions/17255737/importing-variables-from-another-file) – Chris Jan 19 '21 at 00:56
  • Yes, thanks for your help! – Dan Jan 19 '21 at 16:45

1 Answers1

1
import psycopg2

# Database Credentials
DB_HOST = "localhost"
DB_NAME = "movie_watchlist1"
DB_USER = "postgres"
DB_PASS = "postgres123"

def database_credentials():
    return psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASS, host=DB_HOST)
from connect1 import database_credentials

connection = database_credentials()
ElapsedSoul
  • 725
  • 6
  • 18
  • Thank-you that worked perfectly. I see where I made the mistake. – Dan Jan 19 '21 at 16:44
  • @Dan I can tell you are a freshman for programing. First,your function doesn't return anything, and second you need to know what you can return and what are you returned. Acturely you did ```psycopg2.connect``` twice in your code. – ElapsedSoul Jan 20 '21 at 00:44