title: Python: unable connect with database
body: <p>Python 3.4 with psycopg2</p>
  
  <p>I used <a href="https://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL" rel="nofollow">this guide</a> to set up a basic <code>psycopg2</code> connection like so: </p>
  
  <pre><code>#!/usr/bin/python
  import psycopg2
  import sys
  import pprint
  
  def main():
      conn_string = "dbname='CIBTST' host='XX.XX.XXX.XX' port='XXXX' user='XXXXX' password='XXXX'"
      conn = psycopg2.connect(conn_string)
      cursor = conn.cursor()
      cursor.execute("My_select")
      records = cursor.fetchall()
      pprint.pprint(records)
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>I get this traceback:</p>
  
  <pre><code>Traceback (most recent call last):
  File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 14, in &lt;module&gt;
  main()
  File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 8, in main
  conn = psycopg2.connect(conn_string)
  File "C:\Python34\lib\site-packages\psycopg2\__init__.py", line 164, in connect
  conn = _connect(dsn, connection_factory=connection_factory, async=async)
  psycopg2.OperationalError
  </code></pre>
  
  <p>When I try the same select statement in another client (Toad) it seems to operate correctly. Any suggestions will be great. Thank you!</p>
  
  <p>EDIT: The source of problem is: PORT. If I run:</p>
  
  <pre><code>#!/usr/bin/python
  import psycopg2
  import sys
  import pprint
  
  def main():
      conn_string = "dbname='CIBTST' host='XX.XX.XXX.XX' user='XXXXX' password='XXXX'"
      conn = psycopg2.connect(conn_string)
      cursor = conn.cursor()
      cursor.execute("My_select")
      records = cursor.fetchall()
      pprint.pprint(records)
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>Then I get:</p>
  
  <pre><code>Traceback (most recent call last):
  File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 14, in &lt;module&gt;
  main()
  File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 8, in main
  conn = psycopg2.connect(conn_string)
  File "C:\Python34\lib\site-packages\psycopg2\__init__.py", line 164, in connect
  conn = _connect(dsn, connection_factory=connection_factory, async=async)
  psycopg2.OperationalError: could not connect to server: Connection timed out  (0x0000274C/10060)
  Is the server running on host "XXXXXXX" and accepting
  TCP/IP connections on port 5432?
  </code></pre>
  
  <p>I can only connect under port 1522 for some reason. Any ideas?</p>