You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4.0 KiB
4.0 KiB
None
<html lang="en">
<head>
</head>
</html>
Writing to PostgreSQL¶
- Insure PostgreSQL is actually installed on the system,
- There is a database called demo created on the said system
The cell below creates a dataframe that will be stored within postgreSQL
In [8]:
#
# Writing to PostgreSQL database
#
import transport
from transport import providers
import pandas as pd
_data = pd.DataFrame({"name":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})
pgw = transport.factory.instance(provider=providers.POSTGRESQL,database='demo',table='friends',context='write')
pgw.write(_data,if_exists='replace') #-- default is append
print (transport.__version__)
Reading from PostgreSQL¶
The cell below reads the data that has been written by the cell above and computes the average age within a PostreSQL (simple query).
- Basic read of the designated table (friends) created above
- Execute an aggregate SQL against the table
NOTE
It is possible to use transport.factory.instance or transport.instance they are the same. It allows the maintainers to know that we used a factory design pattern.
In [6]:
import transport
from transport import providers
pgr = transport.instance(provider=providers.POSTGRESQL,database='demo',table='friends')
_df = pgr.read()
_query = 'SELECT COUNT(*) _counts, AVG(age) from friends'
_sdf = pgr.read(sql=_query)
print (_df)
print ('--------- STATISTICS ------------')
print (_sdf)
The cell bellow show the content of an auth_file, in this case if the dataset/table in question is not to be shared then you can use auth_file with information associated with the parameters.
NOTE:
The auth_file is intended to be JSON formatted
In [1]:
{
"host":"klingon.io","port":5432,"username":"me","password":"foobar",
"database":"demo","table":"friends"
}
Out[1]:
In [ ]: