2

my setting of database in settings.py is below

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '',
        'PORT':'8081',
    }
}

but when running python manage.py migrate i am getting error django.db.utils.OperationalError: could not connect to server: No such file or directory

Traceback (most recent call last):
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 119, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.8081"?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 93, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/executor.py", line 19, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/loader.py", line 47, in __init__
    self.build_graph()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/loader.py", line 182, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 162, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 135, in _cursor
    self.ensure_connection()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 119, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.8081"?

any help will be helpful thanks in advance I am working on a ubantu os.

Vivek
  • 341
  • 1
  • 5
  • 15

2 Answers2

1

It looks like PostgreSQL is not running at all. Try running

sudo service postgresql start
Oli
  • 1,003
  • 7
  • 17
  • How did you install it? – Oli Sep 27 '15 at 11:11
  • "pip install psycopg2" in my virtualenv – Vivek Sep 27 '15 at 12:29
  • 1
    You'll need to install the actual Postgresql server. Psycopg2 is just a Python adaptor to help you connect to it. Try https://help.ubuntu.com/community/PostgreSQL for a guide to installing it. – Oli Sep 27 '15 at 14:46
1

This steps worked for me Deactivate the virtualenv if there is.

  1. pyenv deactivate

  2. Install the required libraries.

    sudo apt-get install libpq-dev python-dev

  3. Install PostgreSQL.

    sudo apt-get install postgresql postgresql-contrib

  4. Start the psql shell.

    sudo -u postgres psql

  5. Setup the postgres user password by entering the following command and you will be prompted for password in the psql shell. Press Control + D to quit after it is done.

    \password postgres

  6. Create a new database user called django_user.

    `

    sudo -u postgres createuser django_user

  7. Go to the psql shell again.

    sudo -u postgres psql

  8. Create the database and name it django_db.

    CREATE DATABASE django_db;

  9. Set the password for django_user.

    ALTER USER django_user WITH PASSWORD '';

  10. Grant the privilege properly.

    GRANT ALL PRIVILEGES ON DATABASE django_db TO django_user;

  11. Exit the psql shell and activate the virtualenv.

    pyenv activate

  12. Install the psycopg2 package.

    pip install psycopg2

  13. Edit the Django project settings.py as follow.

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django_db', 'USER': 'django_user', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '5432', } }

  14. Initialize the db.

    python manage.py migrate

  15. Start the Django project and see if everything works fine.

    python manage.py runserver 0.0.0.0:8000

Done =)

Vivek
  • 341
  • 1
  • 5
  • 15