Problems in connecting to MusicBrainz database using psycopg2
Question: I am trying to connect to the MusicBrainz database using the psycopg2 python's
module. I have followed the instructions presented on
<http://musicbrainz.org/doc/MusicBrainz_Server/Setup>, but I cannot succeed in
connecting. In particular I am using the following little script:

    
    
    import psycopg2
    conn = psycopg2.connect( database = 'musicbrainz_db', user= 'musicbrainz', password = 'musicbrainz', port = 5000, host='10.16.65.250')
    print "Connection Estabilished"
    

The problem is that when I launch it, it never reaches the print statement,
and the console (I'm on linux) is block indefinitely. It does not even catches
the ctrl-c kill, so I have to kill python itself in another console. What can
cause this?


Answer: You seem to be mistaking MusicBrainz-Server to be only the database. What's
running on port 5000 is the Web Server. You can access
`http://10.16.65.250:5000` in the browser.

Postgres is also running, but listens on `localhost:5432`. This works:

    
    
    import psycopg2
    conn = psycopg2.connect(database="musicbrainz_db",
                            user="musicbrainz", password="musicbrainz",
                            port="5432", host="localhost")
    print("Connection established")
    

In order to make postgres listen to more than localhost you need to change
`listen_addresses` in `/etc/postgresql/9.1/main/postgres.conf` and make an
entry for your (client) host or network in
`/etc/postgresql/9.1/main/pg_hba.conf`.

My VM is running in a 192.168.1.0/24 network so I set `listen_addresses='*'`
in postgres.conf and in pg_hab.conf:

    
    
    host    all             all             192.168.1.0/24          trust
    

I can now connect from my local network to the DB in the VM.

* * *

Depending on what you actually need, you might not want to connect to the
MusicBrainz Server via postgres. There is a [MusicBrainz web
service](http://musicbrainz.org/doc/Development/XML_Web_Service/Version_2) you
can access in the VM. Example:
`http://10.16.65.250:5000/ws/2/artist/c5c2ea1c-4bde-4f4d-bd0b-47b200bf99d6`.
In that case you might be interested in a library to process the data:
[python-musicbrainzngs](http://python-musicbrainzngs.readthedocs.org/).

EDIT: You need to set `musicbrainzngs.set_hostname("10.16.65.250:5000")` for
musicbrainzngs to connect to your local VM.

