0

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?

papafe
  • 2,959
  • 4
  • 41
  • 72

1 Answers1

1

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 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.

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

JonnyJD
  • 2,593
  • 1
  • 28
  • 44
  • 1
    I can reproduce psycopg2 blocking when trying port 5000. This doesn't work since 5000 runs the HTTP protocol, but psycopg2 shouldn't be blocking, but rather give an exception. – JonnyJD Apr 26 '13 at 10:16
  • This was exactly my problem, thank you very much! Regarding the MusicBrainz web service, it seems that the library python-musicbrainz-ngs connects only to the "main" musicbrainz web service and I don't know if I can redirected to the local one. In fact, the main database has a limitation of one web service call per second and, in my case, this would not be enough. – papafe Apr 26 '13 at 11:48
  • 1
    Please have a look at [musicbrainzngs.set_hostname()](https://python-musicbrainz-ngs.readthedocs.org/en/latest/api/#musicbrainzngs.set_hostname). You can also connect to a local MusicBrainz Server. – JonnyJD Apr 26 '13 at 12:17
  • 1
    FYI: I get OperationalError after ca. 10 minutes when trying to access port 5000 with psycopg2. – JonnyJD Apr 26 '13 at 12:18
  • Thanks for the info. In any case I think I will stick to the database because it seems that the library still does not have api functions for retrieving lyrics and relationships. Right now I have a mismatch between the musicbrainz identifier and the name of the artist but I've already asked a question on the musicbrainz forum. – papafe Apr 26 '13 at 13:15
  • Lyrics are not on musicbrainz.org at all and relationships are given when you use the right includes: http://musicbrainz.org/ws/2/artist/c5c2ea1c-4bde-4f4d-bd0b-47b200bf99d6?inc=artist-rels. I wouldn't recommend using the DB directly at all. – JonnyJD Apr 26 '13 at 13:25
  • Sorry, I did not meant lyrics. I want to retrieve the name of their songs. I know that the relationships are retrievable from the web service, but I don't know how to query it without using the library that seems to not have included this functionality yet. I am not an expert in python or web services, so I would need something already done. – papafe Apr 26 '13 at 13:30
  • 1
    Please invest a bit of time to get to know musicbrainzngs. The lib *does* support giving song names and relationships! I will continue the discussion in the forums: http://forums.musicbrainz.org/viewtopic.php?id=4233 – JonnyJD Apr 26 '13 at 13:41