I was wondering if it was possible to use Python libraries inside PL/Python.
What I want to do is remove one node in our setup. Right now we have a sensor publishing data to RabbitMQ using Mosquitto and MQTT.
On the other side, we have PostgreSQL and we want to build a database. I know that we need something between RabbitMQ and PostgreSQL and we were thinking of Paho.
But we were wondering if it was possible to run a script on PostgreSQL using plpython and using the library Paho there. So that would make onr less thing to execute "stand alone".
Or maybe there are other alternatives?
Sure, you can import any module into PL/Python. The documentation states:
PL/Python is only available as an “untrusted” language, meaning it does not offer any way of restricting what users can do in it and is therefore named plpythonu.
Just make sure you don't use multi-threading inside PostgreSQL.
Related
I have a web server which I have developed an application on using php and SQL, mainly picked php as I am more comfortable with it.
In short the application automates some of our network tasks .
As part of this I have automated some solarwinds tasks and the library orionsdk doesnt have a php library so I have used python.
It's all working fine but I really need to run these python scripts from my browser .
I have considered using php shell exec and got my python scripts to accept args so I can run them and parse the output.
I know I could also use flask or django but worry I will have a flask app to maintain aswell as a php app.
I think the question is what the best way to achieve this or any way which I haven't mentioned .
Any help would be very much appreciated
So you want PHP to communicate with Python and you've already mentioned using shell commands and http traffic.
I can imagine you could also achieve something similar by connecting up both PHP and Python up to the same database. In that case PHP could write a record in a table and Python could pick that up and do something with the data in there. Python could be either be a long-running process or fired off by a cronjob in this case. If a database seems overkill you could also write a file to some place on disk, which Python can pick up.
Personally I'd go for the shell exec approach if you want to keep it light weight and for a API connection if you want to have a more robust solution which needs to be expanded later on.
Do you know if it's possible to create a role/user for the Postgresql database from within Python code?
I would prefer to use asyncpg library, since my program is based on asynchronous code, but if there are better libraries for this specific task, I don't mind using them.
I already have a pre-installed database on my server machine, so another possibility would be to just run the Shell command from withing the Python program to create a role. However, I am not sure if you can create a role in just one Shell line.
After some digging, the answer appeared to be very straightforward:
pool.execute("CREATE ROLE name ...")
I have a Django application that runs on apache server and uses Sqlite3 db. I want to access this database remotely using a python script that first ssh to the machine and then access the database.
After a lot of search I understand that we cannot access sqlite db remotely. I don't want to download the db folder using ftp and perform the function, instead I want to access it remotely.
What could be the other possible ways to do this? I don't want to change the database, but am looking for alternate ways to achieve the connection.
Leaving aside the question of whether it is sensible to run a production Django installation against sqlite (it really isn't), you seem to have forgotten that, well, you are actually running Django. That means that Django can be the main interface to your data; and therefore you should write code in Django that enables this.
Luckily, there exists the Django REST Framework that allows you to simply expose your data via HTTP interfaces like GET and POST. That would be a much better solution than accessing it via ssh.
Sqlite needs to access the provided file. So this is more of a filesystem question rather than a python one. You have to find a way for sqlite and python to access the remote directory, be it sftp, sshfs, ftp or whatever. It entirely depends on your remote and local OS. Preferably mount the remote subdirectory on your local filesystem.
You would not need to make a copy of it although if the file is large you might want to consider that option too.
Can i install and build postgreSQL from Source Code in Windows by using Python? Is it solid? Currently in their documentation they have Visual C++ as their only option!
And if it is possible (and reliable) where can i find prime material to use Python to build and customize my postgreSQL? I won't go and learn Visual C++ unless it is the only way. I also have GitBash if that helps...
For people who have no idea on db's. They never say that in documentations because they think it for granted.
IT ALL BOILS DOWN TO: It is another thing building_from_source/installing a db and a totally another thing interacting/working with a db.
There is library for accessing Postgres from python called psycopg. There is a tutorial on its use here.
To the extent that you are just learning about databases, you might also take a look at Sqlite, which is a more lightweight database and included in the python standard library
Since you want to use PostgreSQL you really don't need to compile anything. Simply install PostgreSQL and the windows binaries of psycopg2 (the PostgreSQL client library for python).
If you want to build the PostgreSQL database server - something you only want/need to do if you want to modify PostgreSQL itself, and which is something you probably don't want to do as a beginner - you should use whatever compiler the Postgres developers suggest since they most likely only have project/make files for that compiler.
I need to write a python application with use both of the mysql and sql server is there
a general python module or library that can access both mysql and sql server as DBI with perl or should i use 2 libraries and if yes which libraries do you recommend .
I guess you are looking for SQLAlchemy. You will probably need some time getting into it, but it is invaluable once you have covered the basics.
SQLAlchemy acts as a frontend to other, database-specific libraries using the Python DB-API -- but beyond this, it provides a query builder library that abstracts out differences between databases' SQL syntax and permits programmatic query construction while still offering the full power of SQL.
Python RDBMS modules implement DB-API.
If you are looking for which connection drivers to use, I believe the only one that well work for both [MySQL (driver here) and SQLServer (native to windows/FreeTDS for linux) is pyodbc. #igor has the right idea, though, use SQLAlchemy, you can connect to each database using different engines, but use the same code to interact with both.