We help IT Professionals succeed at work.
Get Started

Python script with password security?

jisoo411 asked
Last Modified: 2016-03-31

I'm writing a Python script that will connect to a postgresql database using the psycopg2 library.  I'm getting around it pretty well but one concern I have is that when connecting to the database host, I have to hard code the username and password into the connection statement.  I would prefer not to do this and would like to somehow refer to an external file for this information and import that user account info then construct the connection string.  That way at least the username/password combo wouldn't be directly visible when looking at the Python code.  Any help or alternative ideas would be very much appreciated.

Example of the psycopg2 connection statement:
import psycopg2

conn_string = "dbname='mydatabase' port='1234' user='joe_shmoe' password = 'joespassword' host='hostaddress.com'"
conn = psycopg2.connect(conn_string)

Open in new window

Watch Question
Data Scientist
Most Valuable Expert 2014
This problem has been solved!
Unlock 3 Answers and 15 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE