資料庫名稱關鍵錯誤

資料庫名稱關鍵錯誤

我正在嘗試在 Ubuntu Bash 命令上運行重複資料刪除庫和此範例。

https://github.com/dedupeio/dedupe-examples/tree/master/pgsql_big_dedupe_example

基本上,我創建了一個名為 Campfin 的資料庫,正如您在列出資料庫名稱時所看到的那樣,它確實存在。

postgres=# \l
                               List of databases
   Name    |    Owner    | Encoding | Collate |  Ctype  |   Access privileges
-----------+-------------+----------+---------+---------+-----------------------
 campfin   | simon       | UTF8     | C.UTF-8 | C.UTF-8 |
 dbname    | owning_user | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres  | postgres    | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres    | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |             |          |         |         | postgres=CTc/postgres
 template1 | postgres    | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |             |          |         |         | postgres=CTc/postgres
(5 rows)

postgres=# \c campfin
You are now connected to database "campfin" as user "postgres".
campfin=# sudo netstat -plunt |grep postmaster
campfin-#  \conninfo
You are connected to database "campfin" as user "postgres" via socket in "/var/run/postgresql" at port "5433".
campfin-#

當我運行我的 python 程式碼:

python3 pgsql_big_dedupe_example_init_db.py

或者

sudo python3 pgsql_big_dedupe_example_init_db.py

這是連接資料庫模式的一部分。

conn = psycopg2.connect(database=db_conf['campfin'],
                        user=db_conf['111'],
                        password=db_conf['111'],
                        host=db_conf['localhost'],
                        port=db_conf['5433'])

回傳我資料庫campfin不存在:

Traceback (most recent call last):
  File "pgsql_big_dedupe_example_init_db.py", line 75, in <module>
    conn = psycopg2.connect(database=db_conf['campfin'],
KeyError: 'campfin'

我在這裡做錯了什麼?問題是否與「透過「/var/run/postgresql」中的套接字」有關?

當我跑步時:

/mnt/c/WINDOWS/system32/virtualenv/dedupe/dedupe-examples/pgsql_big_dedupe_example$ service postgresql status

返回:10/main(連接埠5433):在線

答案1

db_conf['NAME']並不意味著要更改為db_conf['campfin'].這鑰匙NAME價值的吧campfin

在命令列中,運行

export DATABASE_URL=postgres://111:111@localhost/campfin

然後python3 pgsql_big_dedupe_example_init_db.py用原來的程式碼運行...

con = psycopg2.connect(database=db_conf['NAME'],
                   user=db_conf['USER'],
                   password=db_conf['PASSWORD'],
                   host=db_conf['HOST'],
                   cursor_factory=psycopg2.extras.RealDictCursor)

相關內容