Playbooks

Python Script to Query POSTGRESQL Database

Here is a basic Python script to query a PostgreSQL database using the “psycopg2” library. Very useful for data mining.

import psycopg2

conn = psycopg2.connect(
    host="hostname",
    database="database_name",
    user="username",
    password="password"
)

cursor = conn.cursor()

query = "SELECT * FROM table_name"
cursor.execute(query)

result = cursor.fetchall()

for row in result:
    print(row)

conn.close()

Replace the placeholders with the appropriate values for your database:

  • “hostname” is the address of the database server
  • “database_name” is the name of the database you want to connect to
  • “username” and “password” are the credentials used to access the database
  • “table_name” is the name of the table you want to query.

You can modify the SELECT statement to retrieve specific columns or filter the data as desired.

PyJoy

Recent Posts

Python Script to Monitor Network Traffic

Here is a simple Python script to monitor network traffic. import psutil def monitor_network(): #…

2 years ago

Python Script for Web Scraping

Here's a basic example of web scraping in Python using the requests and BeautifulSoup libraries:…

2 years ago

Python Script to SSH and Run Commands

Here's an example of how you can use Python to SSH into a remote server…

2 years ago

Python Code Example with Classes

Here's an example of a Python class that represents a rectangle: class Rectangle: def __init__(self,…

2 years ago

Python Script Example with Main Method in Active Directory

Here is a simple Python script that interacts with Active Directory (AD) using the pyad…

2 years ago

Python Script Example for System Administrators

Here's a Python script to monitor disk usage on a system and send an email…

2 years ago