Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

608
Views
psycopg2.OperationalError: FATAL: database does not exist

I'm trying to populate a couple databases with psycopg2 within a server I am not the root user of (don't know if it's relevant or not). My code looks like

import json
from  psycopg2 import connect

cors = connect(user='jungal01', dbname='course')
req = connect(user="jungal01", dbname='requirement')

core = cors.cursor()
reqs = req.cursor()

with open('gened.json') as gens:
    geneds = json.load(gens)

for i in range(len(geneds)):
    core.execute('''insert into course (number, description, title)
                    values({0}, {1}, {2});''' .format(geneds[i]["number"], geneds[i]['description'], geneds[i]['title'] ))

reqs.execute('''insert into requirement (fulfills)
                values({0});''' .format(geneds[i]['fulfills'] ))
db.commit()

when I execute the code, I get the above pycopg2 error. I know that these particular databases exist, but I just can't figure out why it won't connect to my databases. (side quest, I am also unsure about that commit statement. Should it be in the for loop, or outside of it? It suppose to be database specific?)

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

First, you have db is not a defined variable, so you code shouldn't run completely anyway.

\list on this server is a bunch of databases full of usernames, of which my username is one

Then the following is how you should connect. To a database, not a table, and the regular pattern is to put the database name, and then the user/pass.

A "schema" is a loose term in relational database. Both tables and databases have schemas, but you seem to be expecting to connect to a table, not a database.

So, try this code with an attempt at fixing your indentation and SQL injection problem -- See this documentation

Note that you first must have created the two tables in the database you are connecting to.

import json
from  psycopg2 import connect

username = 'jungal01'
conn = connect(dbname=username, user=username)
cur = conn.cursor()

with open('gened.json') as gens:
    geneds = json.load(gens)

    for g in geneds:
        cur.execute('''insert into course (number, description, title)
                        values(%(number)s, %(description)s, %(title)s);''', g)

        cur.execute('''insert into requirement (fulfills)
                    values(%(fulfills)s);''', g)
    conn.commit()
over 4 years ago · Santiago Trujillo Report

0

Allen, you said: "in postgres, tables are databases." That is absolutely wrong. Your error message results from this misunderstanding. You want to connect to a database, and insert into a table that exists in that database. You're trying to insert into a database -- a nonsensical operation.

over 4 years ago · Santiago Trujillo Report

0

Make sure you are giving the catalog name as database name and not the schema's under catalog.

Catalog is confusing and quite unnecessary. More details below: What's the difference between a catalog and a schema in a relational database?

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!