title: Getting data from table in database
body: <p>I want to extract data from a <code>postgresql</code> database and use that data (in a dataframe format) in a script. Here's my initial try:</p>
  
  <pre><code>from pandas import DataFrame
  import psycopg2
  
  conn = psycopg2.connect(host=host_address, database=name_of_database, user=user_name, password=user_password)
  
  cur = conn.cursor()
  
  cur.execute("SELECT * FROM %s;" % name_of_table)
  
  the_data = cur.fetchall()
  
  colnames = [desc[0] for desc in cur.description]
  
  the_frame = DataFrame(the_data)
  the_frame.columns = colnames
  
  cur.close()
  conn.close()
  </code></pre>
  
  <p><em>Note: I am aware that I should not use "string parameters interpolation (%) to pass variables to a SQL query string", but this works great for me as it is.</em></p>
  
  <p>Would there be a more direct approach to this?</p>
  
  <p><em>Edit: Here's what I used from the selected answer:</em></p>
  
  <pre><code>import pandas as pd
  import sqlalchemy as sq
  
  engine = sq.create_engine("postgresql+psycopg2://username:password@host:port/database")
  
  the_frame = pd.read_sql_table(name_of_table, engine)
  </code></pre>