简体   繁体   中英

SQLAlchemy can't connect to a local database using an absolute path

After much hassle with getting the POSTGRESQL program configured with a user with a capital letter and figuring out how to write out an abs path in windows: set DATABASE_URL=postgresql:///c:\\\\Program Files\\\\PostgreSQL\\\\9.3\\\\data\\\\books.db

(no spaces ! I learned this the hardway !)

in Windows I am still having difficulty connecting to the local databse I created with a test script I have.

within PSQL there exists a database called 'books' which shows up with '\\z'. The default directory for saving databases has not been changed from POSTGRESQL default. and yet when I run the program :

import csv
import os

from sqlalchemy_utils import database_exists
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))


def import_csv():
    f = open("books.csv")
    reader = csv.reader(f)
    for isbn, title, author, year in reader:
        db.execute("INSERT INTO books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
                    {"isbn":isbn, "title":title, "author":author, "year":year})
    db.commit()

import_csv()

Why does this not connect ? ( I have cut out a lot of extraneous code here to show the active parts I am dealing with)

After much hassle with getting the POSTGRESQL program configured with a user with a capital letter and figuring out how to write out an abs path in windows: set DATABASE_URL=postgresql:///c:\\Program Files\\PostgreSQL\\9.3\\data\\books.db

Postgres is a database server. You can't connect directly to one of the underlying files like that. You need to connect to the network service provided by the Postgres database server.

The acceptable SQLAlchemy URLs for connecting to a Postgres database are shown here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM