Django localhost default port already in use with postgres - django

I am receiving a error when running postgres, pgadmin and django.
Port is already in use. I know how to use the sudo command to kill the server, however I am not being shown my databases in postgres. It just shows running when I start the server. When I create a new port database shows but doesn't migration the data. Also when I run migrations to django I get error, table already exist and or table doesn't exist. This happens everytime as if there is a duplicate database and it's not connected to the one showing in pgadmin. I tried dropping and deleting tables in pgadmin, and still get table exist.
How do i fix this? How do I show all databases connected and delete the one that overrides my default port and have postgres show database servers in the app.
Thank you in advance. Long post but I'm stressing and cant find a solution.
Deleting tables in pgadmin, change port number, open postgres first than start pgadmin.

Related

Connect django to legacy postgresql database, already exist and have 1 table

am trying to connect postgresql database with a table already created (legacy).
Not much documentation on the Internet to do so as the database already created. Am afraid to lose the data. Any guide to do that.
Am using Windows and gitbash and have pgadmin4.
Still learning.
Default database in settings file changed to my database but what's next migrate or inspectdb or create tables in django?!
Any help is appreciated.

Django and Elasticbeanstalk: a column that has been deleted on local hasn't been deleted on RDS

I deployed my project into eb and a form submission doesn't work on eb even though it works on local. And I found the cause.
One of the attribute of a model that is not allowed to be null still remains on RDS even though I removed it in the past.
Log error is like this
ERROR: null value in column "a_variable" violates not-null constraint
How can I handle with this problem? Is there a way to remove the column manually on RDS? I am using PostgreSQL as the db engine.
Your RDS database is just a normal PostgreSQL database. If you really need to access it directly, you can:
Create a security group to open up the PostgreSQL port (5432 with TCP) to your current IP address.
Add the security group to your RDS instance (temporarily, don't forget to remove it later)
Use psql on your local machine to connect to your RDS instance, with your django credentials.
Run the normal SQL command (ALTER TABLE table_name DROP COLUMN column_name;) to delete the column. Be careful! you're directly manipulating your database.
To be sure I would check the django-migrations table to see if all migrations were run correctly. you might run into problems with later migrations if that isn't the case.

Accessing DB create by Django using psql

I am using Django (version 1.8.2) along with PostgreSql (v 9.4.4) on python (v2.7.9 - anaconda distribution) to create an application.
I am creating tables in database by defining them in models.py of Django. Once, I have created these tables I am able to view and add content of these tables in the browser (http://127.0.0.1:8000/admin/).
However, when I try to access these tables using psql, I get the error as relation xyz_abc does not exist, where xyz_abc is a table present in database as seen by using \dt.
I am not able to understand why this should be the case and any suggestions on how this tables can be accessed using psql will be really helpful.
Edit
Steps to access the database are -
Open the terminal in max
type 'pqsl' in terminal which opens the psql
\c testdb - this connects to the database which contains the table xyz_abc
once connected the, i try to select everything from the table using select * from xyz_abc;

Django switch from sqlite3 to Postgresql issues

I've recently changed the database server on my project from sqlite3 to Postgresql and I have a few questions that I hope will give an answer to my issues.
I understand that switching from sqlite to Postgres implies that I create the new database and the tables inside it, right? I've done that but I haven't seen any new files created in my project to show me that the database I've made is visible. (Btw, I've changed the database name in settings.py)
I probably should mention that I'm working in a virtual environment and I would like to know if that affects my references in any way. I've tried to import the tables in Django to try and count the number of records in a table but I get the error: "No module named psdemo". (psdemo is my database name and i'm trying to import the table with:
from ps.psdemo import Product
where ps is my application, psdemo is my database and Product the table in the database.
In conclusion I'm trying to get access to my database and tables but I can't manage to find them. I repeat, there is no new database file in my project or in my virtual environment (I've searched thoroughly) but if I use a terminal connection I can connect to my virtual environment and change directories to get to the application folder then if I connect to the Postgresql server I can create the database, the tables and can Insert into them, make queries etc, but I cannot access them from the Django code.
I understand that switching from sqlite to Postgres implies that I create the new database and the tables inside it, right? I've done that but I haven't seen any new files created in my project to show me that the database I've made is visible. (Btw, I've changed the database name in settings.py)
All you have to do with postgres is create the database. Not the tables. Django will create the tables, and anything else it thinks are useful, once you call syncdb.
You won't have any new files in your project like you did in sqlite. If you want to view your database, you should download and install pgadminIII (which I would recommend in any event)
I probably should mention that I'm working in a virtual environment and I would like to know if that affects my references in any way. I've tried to import the tables in Django to try and count the number of records in a table but I get the error: "No module named psdemo". (psdemo is my database name and i'm trying to import the table with:
Here, you import models via normal python syntax and it then references your tables. Each model should represent a single table. You define your models first, and then call
python manage.py syncdb
In conclusion I'm trying to get access to my database and tables but I can't manage to find them.
See above, but you should definitely read about postgres installation from the postgres docs, and read the psycopg2 docs as well as the Django docs for setting up a postgres database.
I understand that switching from sqlite to Postgres implies that I
create the new database and the tables inside it, right? I've done
that but I haven't seen any new files created in my project to show me
that the database I've made is visible. (Btw, I've changed the
database name in settings.py)
Database files are not created in the project directory with postgresql. They are created in the database server data directory (like /var/lib/postgres it depends on the distribution). You should generally query it through a PostgreSQL client that connects to the PostgreSQL server rather than messing with the files directly.
You can for example run command:
manage.py dbshell
As to your first issue, see #jpic's answer.
On your second issue, your database is not a package, and you do not import models from your database. If you were able to import your models correctly before you made any changes, change your import statements back to how they were.

Tables not recognized on restored geodjango postgis backup

So I backed up my geodjango postgis database using pg_dump before performing some calculations which I've managed to mess up. I've created a new database using
createdb -T template0 -O proj proj_backup
psql proj_backup < proj_backup.pg
This seemed to work fine (though there were a few errors during the import), and connecting to the database using psql all my tables are there and appear to have the correct numbers of rows etc.
However, changing my settings.py to connect to my newly imported backup db (proj_backup in my example), gives me the following errors:
DatabaseError: relation "appname_model" does not exist
Any ideas? I'm guessing I did the dump wrong, or that I haven't maintained the ForeignKeys somehow. Thanks very much.
Update
So I figured out my first mistake: I had two similarly named backup databases and connected to the wrong one. Connecting to the correct one seems to have fixed everything. However, it's still quite strange that it didn't recognize the tables in the other backup database, which definitely did exist. Running syncdb on the incorrect database ends up duplicating those tables (if I remember correctly, there were duplicate table names when I listed them all from within psql). Unfortunately, the way I discovered my mistake was by dropping the bad table to recreate it, and so in order to reproduce this error I'll probably have to use time machine. Still very strange, I'll give that a shot when I can get physical access to my work machine.
So is your appname_model table there or is it a view? Was it in public or another named schema?
If the table is there, then chances are you have it in a schema that is not in your database search path. Check the search_path of your old database. It might have included something other than the default, or your default search schema is set in postgresql.conf and is non-standard.