Select in Python (with BD: Postgres)

1

I'm not able to do a select in the PostgreSQL database. Python 3.

conn = psycopg2.connect(host="localhost", dbname='postgres', user='postgres', password=postgres)

cur = conn.cursor()

cur.execute("select id_log, nm_ip, dh_chamada, nm_tipo, nm_contexto, cd_http, in_tamanho from tb_log")

cur.fetchone()

Error result:

cur.execute("select id_log, nm_ip, dh_chamada, nm_tipo, nm_contexto, cd_http, in_tamanho from tb_log")
psycopg2.ProgrammingError: relation "tb_log" does not exist
LINE 1: ...amada, nm_tipo, nm_contexto, cd_http, in_tamanho from tb_log
                                                                 ^

Base PostGres:

SELECT id_log, nm_ip, dh_chamada, nm_tipo, nm_contexto, cd_http, in_tamanho
        FROM tb_log;

Result:

1, '12 ',' 2018-06-07 13:03:13 ',' HEAD ',' / FDSFDS ', 200 , 1000

    
asked by anonymous 11.06.2018 / 15:51

2 answers

2

Check in which schema table tb_log is contained! For example, if your table is contained in a schema named xpto , your query looks like this:

SELECT
    id_log,
    nm_ip,
    dh_chamada,
    nm_tipo,
    nm_contexto,
    cd_http,
    in_tamanho
FROM
    xpto.tb_log;

Certainly the application or ROLE that the application is using to run the query does not have the environment variable SEARCH_PATH set correctly.

To set the environment variable manually during the session, and make the schema % accessible_company:

SET SEARCH_PATH = xpto, public;

To set the environment variable permanently to a given xpto :

ALTER ROLE app_role SET search_path = xpto, public;
    
11.06.2018 / 16:24
0

Dear Anderson - I've changed the line

conn = psycopg2.connect (host="localhost", dbname = 'postgres', user = 'postgres', password = postgres)

To:

conn = psycopg2.connect (host="localhost", database = 'SIPJE', user = 'postgres', password = postgres)

And it worked ... Many thanks.

    
11.06.2018 / 19:36