Django app switch from Mariadb to Postgresql - django

I have changed the Django Settings file database engine to postgres_psycopg2.
When I run ./manage.py run server I get the following error numerous times:
"[03/Apr/2019 18:40:09] code 400, message Bad request version
('error",
"name":"DatabaseError",
"code":"ECONNREFUSED",
"property":null,
"redirect":null,
"errno":"ECONNREFUSED",
"syscall":"connect",
"address":"127.0.0.1",
"port":3306,"fatal":true}}'
)
For some reason it is trying to connect to port 3306, which is the Mariadb port and not port 5432, which is the port for Postgresql.
I just upgraded my Mariadb to version 10.3.14 but the problem still exists. My Postgresql is version 11.2.
I have been Googling for several days now and still have not found a solution.
EDIT: I changed the "./manage.py runserver 192.168.1.109:8000/admin" and the error went away. In my /etc/mysql/my.cnf file it had the bind_address set to the above ip address. I comment out that statement and now all is cool.

Related

migration problem while hosting a django project on PythonAnywhere

I recently finished my django project and i switched my database from the local SQLite database, provided by django, to an online postgreSQL database provided by elephantSQL. Clearly, by entering my database credential in the setings.py file of my django project i am able to migrate to my new database locally while not on my host's bash console. Meaning that, when i run my server on port 8000 it loads my data from the postgreSQL, but when i push my website to PythonAnywhere and try to run my migrations from the bash console of PythonAnywhere it throws out the following error:`
django.db.utils.OperationalError: connection to server at "'USER'.db.elephantsql.com" ("DATABASE_IP"), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
`
I even changed my database Host from elephanSQL to nhost, but still it promps the same result.

Unable to connect to server: PgAdmin 4

I have installed pgadmin on a new windows laptop and when I try to create a new server, it says:
When I try to run my django app in pycharm it is giving me the same error
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
How to solve this ?
In case someone is running the pgadmin-4 in docker, and not able to connect to postgres container, like me.
The solution is to first find the ip at which the docker image is running.
Step-1, make sure the postgres container is running.
Step-2 write command- PS C:\docker> docker ps
Should result as below or similar,
Step3- in order to find the ip address running the postgres use part of container ID and analyze like below command
PS C:\docker> docker inspect fc834
Note: Here I have only used part of container id that is fc834..
This should result the following or similar,
Step4-
Use this ip address in the connection as below with your correct username and password
You may need to installing PostgreSQL Server first.
You can verify if the folder is created in the below folder,
C:\Program Files\PostgreSQL
You can configurate your newly created server to run on localhost and port 5432.
First select the “Connection” tab in the “Create-Server” window. Then, configure the connection as follows:
Enter your server’s IP address in the “Hostname/ Address” field. Default is localhost.
Specify the “Port” as “5432”.
Enter the name of the database in the “Database Maintenance” field.
Enter your username as postgres and password (use the same password you used when previously configuring the server to accept remote connections) for the database.
Click “Save” to apply the configuration.
NOTE You first have to install PostgreSQL on your machine and run it or run it with docker.
I had the same issue. But in my case I had installed pgadmin in version 9. But also installed version 12 at the same time.
When I now uninstalled version 9, the port was already set in the config of version 12 and not given free.
So my solution was to change the port of version 12 in the postgresql.conf file. Or even simplier, change the port in the server creation from 5432 to 5433. Now you are able to create a server again.
You should uninstall Postgres and pgAdmin from your PC. Then install postgres, note that you have the option of installing pgAdmin together with Postgres, so you don't have to download pgAdmin separately. Allow the installation to complete then restart you PC. Hopefully you should be able to create your server/database
I was running postgress and pgadmin both using docker container.
sudo docker ps
sudo docker inspect <postgress_container_id>
Output:
"Networks": {
"work_file_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"postgres",
"578a7a1050d1"
],
"NetworkID": "49dbe9d7280b55e36afc4308469c1b55e051d7eea8f1c03f08728e652cf22b5b",
"EndpointID": "c30a642c5a0f2970147c9734cadfbe1e8d7c29fcba8a83a628b7c2b3db114716",
"Gateway": "172.18.0.1",
**"IPAddress": "172.18.0.4",**
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:04",
"DriverOpts": null
}
Instead of localhost put the IP obtained from above command (172.18.0.4)
In my case was I got both pgadmin and postgresql services running in separate docker containers and I was trying to connect to localhost(127.0.0.1), which is cause of unable to connect to server error.
Note: 5438 port on my computer (host machine) was mapped to 5432 port of postgresql container.
so practically there are two solutions (if you have these services in separate containers and you have mapped postgresql port to your host machine ):
1-find out your local IP (mine is 192.168.1.106) and put it in the Host field.
2- you can put two containers(pgadmin and postgres) in one network (docker network)
and instead of your local IP, put postgres container IP in the Host field.
-Another tip that may help: what I've recently find out was if you are linux user and have ufw enabled, you should allow the port.
e.g. on my computer postgres is running on 5438 port, so I performed below command (so I could connect from pgadmin container to 5438 port of host wich postgres is running)
ufw allow 5438
Execute the container with the data Eg:
docker run --name postgresdb -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password -e POSTGRES_DB=mydb -p 5432:5432 --restart always -d postgres
Then in the PGAdmin client in the Host Name/Address use:
host.docker.internal
Image Conn PGAdmin
I was trying to install PostgreSQL and pgAdmin with an installer that is given here https://www.postgresql.org/download/windows/. This installer includes the PostgreSQL server, pgAdmin;
I was facing an error while starting pgAdmin: "The pgAdmin 4 server could not be contacted". I tried different solutions but did not work.
Then I uninstalled both of them, deleted the temp folder C:\Users\%USERNAME%\AppData\Roaming\pgAdmin and delete those ones too %temp%.
Then I installed the pgAdmin separately from this link https://www.pgadmin.org/download/
and it works. If you need to connect it with your local server I think you should install the PostgreSQL server first and then pgAdmin separately.
I faced the same problem. So I uninstalled pgAdmin through control panel. after that deleted the folder where pgAdmin was located. Then I went to this link and installed pgadmin whole package from there and now it works fine.
I was getting this error when I was running pgadmin in a docker container on my machine, which meant that localhost:5432 was not accessible.
I worked around this by using the native version of pgadmin.
If you are running PostgreSQL in a docker container, set the host name in pgAdmin to postgres not the mapped address or localhost.
press win key+R then Search for services.msc A window will open in that find postgresql-x64-13 right click, in that tab click start option For me its works perfectly.
Check out this stackoverflow link
unable to connect to server for Postgres
how I solved this problem in ubuntu 22.04
I didn't have a password set in Postgres, that's why the error occurred 'unable to connect server 127.0.0.0 port 5432
Open the terminal in ubuntu and enter this command;
sudo -u postgres psql
Run the statement to add new password. ALTER USER Postgres PASSWORD 'AddNewPasswordHere'; in '' you should enter your new password
Example:
1)sudo -u postgres psql
2)ALTER USER postgres PASSWORD 'mynewpassword'
3)sudo service postgresql restart
4)Then you can create a server in pgadmin
If you already tried with “127.0.0.1” and it didn’t work then use “localhost”
After two years i think this would be of good help to so many people.
You don't have to uninstall postgresql or PGADMIN from your system.
What you need to do i input the username and password for a particular user created on postgresql into the server input box.
And that all you need.
i hope this helps anyone

Local Django website won't load in browser

I'm guessing there's a very simple solution to this, but I searched every forum and setup guide and can't figure it out:
I built a Django/CentOS-6.3 environment on my local server (using VirtualBox and Vagrant). When I startup my server in the vagrant terminal with 'python manage.py runserver [::]:8000' it starts up with no errors.
Validating models...
0 errors found
May 31, 2013 - 13:56:15
Django version 1.5.1, using settings 'mysitename.settings'
Development server is running at http://[::]:8000/
Quit the server with CONTROL-C.
However, when I try to navigate to 'http://127.0.0.1:8001' in my browser (I set up port forwarding from port 8000 to port 8001 in my Vagrantfile), the browser just hangs for 5 minutes until it times out, then it returns the message:
> The connection was reset
> The connection to the server was reset while the page was loading.
> ...
This is the exact same message I get from the browser even after I shut down my local server. My computer obviously recognizes this as a forwarded port, because any other port I try (such as 8000) instantly returns an error saying that it can't establish a connection to the server at 127.0.0.1:8000.
With regard to the server files, I have done many similar setups with Django/Ubuntu in the past and have never had any issues, but there must be something different about Django/CentOS that is causing this to happen (or maybe I made a mistake someone in one of my server files). I have followed guides for setting up Django & PostgreSQL on CentOS, too, but to no avail. I'll comment some of the files I have created/edited below.
If anyone has a solution, or even has advice on where to start looking for errors, I would very much appreciate it.
If your network is configured correctly and your django application with
python manage.py runserver 0.0.0.0:8000
and you still can't access your django app from the VM host there is almost certainly a firewall issue. The solution above is good if you are running iptables.
I deployed CentOS 7 on a virtualbox VM from a Windows 7 host. I didn't know that this distribution uses firewalld, not iptables to control access.
if
ps -ae | grep firewall
returns something like
602 ? 00:00:00 firewalld
your system is running firewalld, not iptables. They do not run together.
To correct you VM so you can access your django site from the host use the commands:
firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --reload
Many thanks to pablo v for pointing this out in the post "Access django server on virtual Machine".
the host's "127.0.0.1" is not the same as the guest's "127.0.0.1". Per default the command
python manage.py runserver
listens only to the guest's localhost. You should be able to test it from within the vm (use "vagrant ssh" to login) and run
curl -I http://127.0.0.1:8000/
The host as a different IP. To access the development server from the host you have to start it without ip restriction:
python manage.py runserver 0.0.0.0:8000
Yes:
python manage.py runserver [::]:8000
should be the same. But that's IPv6 syntax AFAIK. Are you sure that the "manage.py runserver" command supports IPv6 by default? I've never used ipv6 addresses w/ django, but looking at the source (https://github.com/django/django/blob/master/django/core/management/commands/runserver.py) there seams to be a flag that the default to False ("--ipv6"). Perhaps that's the "real" problem?
Regards,
For a similar problem,
This command worked like a charm for me
python manage.py runserver [::]:8001
Check your iptables, and stop it. Ubuntu commonly does not open the iptables when it starts.

Error: That IP address can't be assigned-to in Django?

I am running Debian 6 stable and I am trying to run Django locally using ./manage.py runserver command. This is what I get:
Validating models...
0 errors found
Django version 1.4.1, using settings 'genelaytics.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: That IP address can't be assigned-to.
I tried creating a new django project, still get the same error. What's the problem? I just fresh re-installed Debian yesterday because of this problem. This problem hasn't gone away. How can I fix it?
That's not related to Django but to your network configuraton. The very basic loopback interface is not working properly. If you want to learn more about networking and this layer.
Your problem is that your host cannot ping to itself, and so Django gets an error when getting the network address. That's in django/core/management/commands/runserver.py and is returned when it gets the system error 99 (EADDRNOTAVAIL).
Your network configuration at /etc/network/interfaces should have this piece at the beginning:
# The loopback network interface
auto lo
iface lo inet loopback
Check Debian documentation for full network configuration.
You are missing two lines of code in network configuration file. Edit the file as a root user with any text editor:
sudo gedit /etc/network/interfaces
Add these two lines:
auto lo
iface lo inet loopback
Restart the network using the following command:
sudo /etc/init.d/networking restart
Now, the localserver will work fine.

PostgreSQL Socket Error after Installation

I installed PostgreSQL(9.0.7) from EnterpriseDB on OS X 10.6. However, when I do a simple
$ psql -U postgres
I get this
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I am using it with Django. I tried changing the host variable in settings.py to
'HOST': '/tmp'
but I am still getting the same error message when I do a syndb. Any ideas? Thank you.
In my postgresql.conf
listen_addresses = '*'
port = 5432
unix_socket_directory = '/var/pgsql_socket/'
Edit:
I got it working. For some reason installed the newer version 9.1.3 and that error went away. I also got some help from a cheat sheet for PostgreSQL(http://od-eon.com/blogs/calvin/postgresql-cheat-sheet-beginners/)
Have you started the database cluster? You should be able to tell by running the command
ps auwwx|grep postg
You should see several postgresql processes. If you do not, you need to start the database server.
http://www.postgresql.org/docs/9.0/static/server-start.html