Select in Python (with BD: Postgres)

Asked

Viewed 671 times

1

I am unable to 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;

Upshot:

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

  • Are you sure you selected the same database in both examples?

  • Yeah. The same comic

2 answers

2


Check in which schema the table tb_log is contained! For example, if your table is contained in a schema called xpto, your consultation would look 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 the ROLE that the application is using to run the query, not with the environment variable SEARCH_PATH set correctly.

To manually set the environment variable during the session, and make the schema xpto accessible:

SET SEARCH_PATH = xpto, public;

To set the environment variable permanently for a given ROLE:

ALTER ROLE app_role SET search_path = xpto, public;
  • Continua com erro:
Continua com erro - Criei a tabela em um novo schema, setei a variável ambiente e nada

 c.execute("SELECT id_log, nm_ip, dh_chamada, nm_tipo, nm_contexto, cd_http, in_tamanho FROM sipje.tb_log;")
psycopg2.ProgrammingError: relation "sipje.tb_log" does not exist LINE 1: ...a, nm_type, nm_context, cd_http, in_size FROM sipje.tb_l...

  • @Alexsandro: The name of schema in which the tb_log is contained is called even sipje ?! You’re not confusing the name of database with the name of schema ?

  • Hello @Lacobus the bank is called SIPJE and the schema for now has the same name - But I have already solved - as published above. Thank you so much for your attention. I will keep this information about ROLE and the environment variables.

0

Dear Anderson - I changed the line

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

for:

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

And it worked ... Thank you very much.

  • So it wasn’t the same bank :D

  • Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.