Last active
December 8, 2022 22:13
-
-
Save tym-xqo/33baf67bb332cebc4b20f7211dbedf59 to your computer and use it in GitHub Desktop.
pg_service.conf + psycopg2 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3-alpine | |
# install requirements for psycopg2 | |
RUN apk update && apk add -u \ | |
postgresql-dev \ | |
gcc \ | |
python3-dev \ | |
musl-dev | |
RUN pip3 install psycopg2 | |
ENV PGSYSCONFDIR=/usr/etc/postgresql | |
ADD pg_service.conf /usr/etc/postgresql/pg_service.conf | |
WORKDIR /app | |
ADD example.py /app | |
CMD ["python3", "example.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import psycopg2 | |
def service_details(): | |
con = psycopg2.connect(service='example') | |
cur = con.cursor() | |
cur.execute('select version() as version ' | |
', current_database() as db ' | |
', current_user as user ' | |
', now()::text as query_time ' | |
) | |
result = cur.fetchone() | |
return result | |
if __name__ == '__main__': | |
x = service_details() | |
print(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[example] | |
host=warehouse.pgdata.xyz | |
dbname=warehouse | |
port=5433 | |
user=etl_user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker build -t pgexample . | |
$ docker run -e PGPASSWORD=XXXXXXX pgexample | |
('PostgreSQL 9.4.4 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit', 'warehouse', 'etl_user', '2016-07-13 17:41:30.739065+00') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment