-
Notifications
You must be signed in to change notification settings - Fork 0
Postgre SQL
Shamik edited this page Mar 19, 2026
·
1 revision
- Install pgAdmin4 for UI or psql for command line to interact with postgre DB
- Connect to the PostgreSQL server
- Create a new DB by right clicking on the server
- Right click on the schemas under the DB to create a new schema if it doesn’t exist
- Under the schemas, right click on the tables icon and create a table
- Create the necessary columns of the tables and save the table
- Right click on the table and choose scripts
- Click on scripts and select INSERT script
- Insert the data in the insert panel and execute it
import psycopg2
db_params = {
"dbname": "mydb",
"user": "postgres",
"password": "1234",
"host": "localhost", # or your PostgreSQL server's hostname
"port": 5432, # default PostgreSQL port
}
try:
conn = psycopg2.connect(**db_params)
print("Connected successfully!")
# You can now perform SQL queries using 'conn'
except psycopg2.Error as e:
print(f"Error connecting to the database: {e}")
cursor = conn.cursor()
# Execute a sql query
cursor = conn.cursor()
cursor.execute("SELECT * FROM linkage LIMIT 2;")
for i in (rows:= cursor.fetchall()):
print(i)Either through the psql command line with the following command:
\copy public.polling_data (id, candidate_id, state_id, pollster_id, rcp_average, spread, polling_date, polling_month, polling_year, day_diff, month_diff, candidate_affiliation, region, states, candidate_name) FROM '/Users/shamik/data/rcp/processed_data/trial.csv' DELIMITER ',' CSV HEADER ENCODING 'UTF8' QUOTE '''' ESCAPE '''';OR through PgAdmin:
- Select the table and right click on it
- Select import/export data
- Select the file path and fill in all the other details
- Press ok to import
- Select table and right click
- Select ”backup” and specify the filename
- Choose “plain” format
- Press “backup”
- Open psql command line
- Use the following command
\i /Users/shamik/data/rcp/processed_data/linkage.sql - Refresh the tables