Filament Manager Plugin: How to setup postgresql database

Ok....There is a lot of good advice on here and around the web that can help you figure this out, but it can be a daunting task too! So, after MUCH trial and error, research and some luck, I got it figured out and wanted to share a concise tutorial on how to do it.

First...why would you want to do this? So that you can share the spool data across multiple instances of Octoprint and they all access and update the remaining amount of filament, no matter which instance or machine you're printing from.

So...here we go and it's really not that hard! :wink:

First...disable any THEMES or UI changes you may have! Why? because they may possible disable the TEST connection button from changing colors and you'll never get the feedback (green is good, red is bad) you need to know if you've successfully connected or not. Trust me on this one...this one kept me thinking it wasn't connecting for a LONG while, and it was my damn theme! I only figured this one out because I started all over on a fresh install of Octoprint and ONLY installed this mod and I noticed the RED (failed to connect) before due to my theme. This also means, that if I was previously successful, I never saw the GREEN (successful) one either!

Now, with that out of the way. Here we go...for real this time :smile:

  1. Download and install Putty terminal application to SSL into your Pi. You may use other tools, but I am most comfortable with this one, and it works great.
  2. sudo apt-get update Lets make sure we have all the latest updates!
  3. sudo apt-get upgrade Install them!
  4. sudo reboot now Reboot the Pi.
  5. ~/oprint/bin/pip install --upgrade pip Let's just upgrade pip while we're at it.
  6. sudo reboot now Not sure if it's necessary, but can't hurt!
  7. sudo apt-get install libpq-dev python-dev Install these dependencies first!
  8. ~/oprint/bin/pip install psycopg2 Now install psycopg2 (required as well)
  9. sudo reboot now yeah, yea....again...just to be safe.
  10. sudo apt-get install postgresql Install postgresql
  11. sudo -u postgres -i become postgres user so we can create the octoprint user and database!
  12. Now...your prompt will have changed, so next lets create the user via the interactive feature:
    createuser --interactive -P
    Enter the username: "octoprint", and answer "n" to all the next questions about this user.
  13. Now we create the database itself: createdb -O octoprint octoprint_filamentmanager
    14 Now exit the user and back to the main prompt by typing: exit
    15 Now we have to edit two files, the first one is here
    sudo nano /etc/postgresql/9.6/main/postgresql.conf
    In this file, find the line "listen_addresses = " and change it to

listen_address = '*'

save your changes, then onto the next using:
sudo nano /etc/postgresql/9.6/main/pg_hba.conf

The Wiki says it best here, so you do this:
Then append the following line to /var/lib/postgres/data/pg_hba.conf

host octoprint_filamentmanager octoprint 192.168.178.0/24 md5

Adapt the IP address to your network, e.g. if your server has the IP 192.168.0.25 use 192.168.0.0/24 instead. This allows all clients in your network to access the database.

  1. Last step...is to restart the service using: sudo systemctl restart postgresql.service

Now just enter the information into the database settings in Filament manager plugin, just like you set it up, and like the Wiki explains:

Picture shown at the bottom of this wiki page. Of course, enter in the IP address of YOUR Raspberry Pi that's running the database (it can be the same one you're running your octoprint on)

Once you click Test Connection button...if everything has gone correctly, the button will turn green, and you'll need to restart Octoprint to save the settings.

Good Luck!!!

Steps 5 and 8 haven't been done while under the virtual environment of OctoPrint, btw.

If you wanted the working pip and python environment to be affected, you'd first need to do this before a pip-related command (and again if there was another reboot):

source ~/oprint/bin/activate

But then again, does postgresql itself run from that virtual environment or not? If not, then your instructions would work for that. (But the virtual environment might also need to be updated for the sake of any plugins you write.)

i dont get the test green. i dont understand which ip i have to set in the pg_hba.conf
My raspberry pi has the ip 192.168.188.44. can somebody help me? all the commands here in the manual worked but the test button doesnt get green.

ok on the server it works but the client my 2nd pi cannot connect to the database. what i have to do or install on the 2nd pi that he can connect and use the database?

On the second pi, you should only need to do steps 1 through 9. No need to install postgresql on the second pi, but it will need the other stuff. Also, after a reboot I still sometimes have issues reconnecting. What I generally have to do is just restart the octopi instances a time or two but then they eventually both connect fine... It's weird.

I just installed a different database server (MongoDB) on a Pi yesterday. I was surprised to see that the default installation just bound it to the localhost port of 127.0.0.1 and not to all IP addresses on that machine. So in my case, I had to edit that to 0.0.0.0 which means (all addresses).

You might need to edit your pg_hba.conf so try researching this.

It works now but i have a new Problem. Now filament manager doesnt subtract the consumed Filament.

If it were me, I'd check the postgresql log to see if it indicates some sort of error. I'd also consider adjusting the log level for this plugin in the Settings area and then look at the octoprint.log file itself. In fact, it is probably logging any errors there already.

And please, keep this to a single thread.