-->

python manage.py dbshell doesn't find psycopg2

2019-09-02 21:30发布

问题:

I'd like to be issuing SQL commands via python manage.py dbshell but I get the error "CommandError: You appear not to have the 'sqlite3' program installed or on your path". From the python prompt I can import psycopg2 without getting any errors and psycopg2 appears to be in my python path.

I'm trying django and my settings.py specifies "ENGINE": "django.db.backends.postgresql_psycopg2"

Any help would be greatly appreciated,

Thanks, Julian

回答1:

when you run ./manage.py dbshell psql or sqlite3 this command assumes the programs are on your PATH. this will simply calls the program name.

sqlite3

from Python itself dosen't contain a sqlite3 command. if you want to access from the command line you have to install SQLite library that includes a simple command-line utility named sqlite3. but without installing library you can create the db server because it doesn’t require running a separate server and sqlite3 itself a text based file.

If you are developing a simple project or something you don’t plan to deploy in a production environment, then use SQLite

in your case first install sqlite3 using sudo apt-get install sqlite3 libsqlite3-dev to access dbshell.

postgres

compare to sqlite3 this postgres needs package to initialize. postgresql need postgresql_psycopg2 package



回答2:

You need to install sqlite3 to be able to run dbshell

Installing the sqlite program should solve the problem:

sudo apt-get install sqlite3


回答3:

I think there is some confusion as to what dbshell does. From the documentation:

Runs the command-line client for the database engine specified in your ENGINE setting, with the connection parameters specified in your USER, PASSWORD, etc., settings.

  • For PostgreSQL, this runs the psql command-line client.
  • For MySQL, this runs the mysql command-line client.
  • For SQLite, this runs the sqlite3 command-line client.

This command assumes the programs are on your PATH so that a simple call to the program name (psql, mysql, sqlite3) will find the program in the right place. There’s no way to specify the location of the program manually.

Its not like manage.py shell, dbshell just runs the normal shell for the database configured, except it logs you in with the credentials in settings.py.

This means, if you are using postgresql, you need to have the psql command already installed in order for dbshell to work. This command is not part of Python, but comes with the postgresql server.

In order words - if all you have installed is psycopg2, the dbshell command will not work.