Skip to content

Commit

Permalink
Merge pull request #261 from yldoctrine/add_postgresql_schema
Browse files Browse the repository at this point in the history
Add the possibility to define a schema for postgresql database
  • Loading branch information
fhamborg authored Jun 4, 2024
2 parents 23d7f17 + dbe5495 commit 7d53952
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ news-please allows for storing of articles to a PostgreSQL database, including t
host = localhost
port = 5432
database = 'news-please'
# schema = 'news-please'
user = 'user'
password = 'password'

Expand Down
2 changes: 2 additions & 0 deletions newsplease/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,12 @@ def reset_postgresql(self):
print("Resetting database...")

try:
options = f"-c search_path={self.postgresql.get('schema')}" if self.postgresql.get('schema') else ''
# initialize DB connection
self.conn = psycopg2.connect(host=self.postgresql["host"],
port=self.postgresql["port"],
database=self.postgresql["database"],
options=options,
user=self.postgresql["user"],
password=self.postgresql["password"])
self.cursor = self.conn.cursor()
Expand Down
1 change: 1 addition & 0 deletions newsplease/config/config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ password = 'password'
host = localhost
port = 5432
database = 'news-please'
# schema = 'news-please'
user = 'root'
password = 'password'

Expand Down
1 change: 1 addition & 0 deletions newsplease/config/config_lib.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ password = 'password'
host = localhost
port = 5432
database = 'news-please'
# schema = 'news-please'
user = 'root'
password = 'password'

Expand Down
2 changes: 2 additions & 0 deletions newsplease/pipeline/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,13 @@ def __init__(self):
self.log = logging.getLogger(__name__)
self.cfg = CrawlerConfig.get_instance()
self.database = self.cfg.section("Postgresql")
options = f"-c search_path={self.database.get('schema')}" if self.database.get('schema') else ''
# Establish DB connection
# Closing of the connection is handled once the spider closes
self.conn = psycopg2.connect(host=self.database["host"],
port=self.database["port"],
database=self.database["database"],
options=options,
user=self.database["user"],
password=self.database["password"])
self.cursor = self.conn.cursor()
Expand Down

0 comments on commit 7d53952

Please sign in to comment.