django/postgres error:'database XX does not exist' - django

I created a database using pgAdmin GUI tool to use with postgres for my django project. There are only two databases in pgAdmin, the default 'postgres' db you get out of the box with pgAdmin, and my new database, dbfunk.
I'm using django and added postgres as my database and gave the necessary info in settings.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbfunk',
'USER': 'postgres',
'PASSWORD': 'XXXXXX',
'HOST': 'localhost'
}
}
But when I run
python manage.py makemigrations
it gives the error, the database 'dbfunk' does not exist, even though it's in both settings.py in Django and added in pgAdmin. The full text of the error is:
django.db.utils.OperationalError: FATAL: database "dbfunk" does not exist
I've installed the adaptor psycopg2. I also tried 'ENGINE': 'django.db.backends.postgresql_psycopg2', in Settings.py but this didn't make any difference.
Is there something else I am missing?
When I ran the command psql \u it is only showing the postgres database there also, and not this new database 'dbfunk'.
I don't know if this helps, but upon installing postgres, I was given 5433 in the prompt as the port number.
UPDATE: I just ran createdb dbfunk from command line and that seems to have created it as I can now run python manage.py makemigrations. But why did I need to do that when I had already done this in pgAdmin? That is, why did I have to create it twice? Is this usual?
UPDATE2: Sadly the database dbfunk that I created from command line using createdb and the dbfunk in pgAdmin are not synchronised and migrations won't carry over, such that I don't see the tables for the django models in pgAdmin.
I installed Postgres initially using Homebrew, and pgAdmin separately but then it didn't provide the default server/database on pgAdmin so I deleted pgAdmin, and uninstalled Postgres, and then downloaded Postgres from the website instead, and it seems that installs pgAdmin alongside it because then when I opened pgAdmin it had the default server/db postgres. I am on macOS, 10.14.6.
When I check ports on 5432 I get this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 16337 me 5u IPv4 0x7560dce8e7fXXXX 0t0 TCP localhost:postgresql (LISTEN)
postgres 16337 me 6u IPv6 0x7560dce8d2XXXXX 0t0 TCP localhost:postgresql (LISTEN)
And on 5433 there are more entries:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 16809 postgres 4u IPv6 0x7560dce8e883ed93 0t0 TCP *:pyrrho (LISTEN)
postgres 16809 postgres 5u IPv4 0x7560dce8e46dXXXX 0t0 TCP *:pyrrho (LISTEN)
pgAdmin4 17043 me 20u IPv4 0x7560dce8e74cXXXx 0t0 TCP localhost:56820->localhost:pyrrho (ESTABLISHED)
pgAdmin4 17043 me 21u IPv4 0x7560dce8e74cXXXX 0t0 TCP localhost:57054->localhost:pyrrho (ESTABLISHED)
postgres 17051 postgres 12u IPv4 0x7560dce8e74cXXXX 0t0 TCP localhost:pyrrho->localhost:56820 (ESTABLISHED)
postgres 17217 postgres 12u IPv4 0x7560dce8e74cXXXX 0t0 TCP localhost:pyrrho->localhost:57054 (ESTABLISHED)

For starters you did not specify PORT in DATABASES. The default port for postgres is 5432, so if your database cluster that contained dbfunk was listening on port 5433 then Django would not find it. My guess is you have two instances of postgres running, one on port 5432 and the other on 5433. When you did createdb dbfunk I'm guessing you again did not specify a port and the database was created in the cluster listening on 5432. Now python manage.py makemigrations could find it using the settings in DATABASES.

Related

why my remote server not able to connect to postgres instance on another Django server?

I have two Ubuntu servers A and B.
Initially I had Django, postgres, celery on single server A. Everything was working fine.
Now A has Django, postgres, and B has celery and Django project installed.
DB migrations are done on A, and database connectivity is proper on A. This I can say that because when I am trying to start the celery server on server A, I am able to successfully launch the celery through the command prompt. No problem.
As per the requirements, I have to only run Django and postgres on server A, and Celery on server B.
Since I am using the same Django server settings on server B that I have in A, so the Django settings.py has the same DB connectivity settings as shown below. i.e. the DB settings are same on A and B
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'xyz',
'USER': 'smthing',
'PASSWORD': 'smthing',
'HOST': 'some_IP',
'PORT': '5432',
}
}
To see if the postgres is running or not on server A. I executed the service command
$ service postgresql status
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2021-06-10 10:57:11 IST; 1 weeks 4 days ago
Main PID: 1276 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4915)
CGroup: /system.slice/postgresql.service
To see if the postgres is listing on 5432, I executed netstat command:
$ netstat -nl | grep postgres
unix 2 [ ACC ] STREAM LISTENING 17357 /var/run/postgresql/.s.PGSQL.5432
If all these are happening, then why server B is throwing error as below when I am trying to start the celery?
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "server A" and accepting
TCP/IP connections on port 5432?
One thing I noticed that when I tried to telnet server A from server B, I get failed output.
telnet server A 5432
[sudo] password for username:
Trying server A...
telnet: Unable to connect to remote host: Connection refused
_ Also, please let me know if I need to have the username and password created on server B as well? i.e. the username and password which is present as a postgres user on server A.

How to connect to local PostGIS (PostgreS) database using Docker compose in Django project?

I need to connect to existing db in my computer. When I create db service with postgis image, it creates a new database inside docker container. But I need to connect to my local one. Is it possible?
docker-compose.yml :
version: '3'
services:
build: ./
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py collectstatic --noinput --i rest_framework && gunicorn orion-amr.wsgi:application --bind 0.0.0.0:8000 --workers=2"
ports:
- "${webport}:8000"
env_file:
- ./.env
settings.py :
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': os.environ.get('pgdatabase'),
'USER': os.environ.get('pguser'),
'PASSWORD': os.environ.get('pgpassword'),
'HOST': os.environ.get('pghostname'),
'PORT': '5432',
}
}
.env file:
pgport=5432
webport=8000
SECRET_KEY=z)mf(y#w7-_8y1)0(v*n#w#lzf)!0=g_rj5$%1w6g-t!7nbk05
pgdatabase=amr_or
pgpassword=root
pghostname=localhost
pguser=postgres
DEBUG=
Now I have this error:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 | could not connect to server: Cannot assign requested address
web_1 | Is the server running on host "localhost" (::1) and accepting
web_1 | TCP/IP connections on port 5432
I really hope you did not expose your real SECRET_KEY to the rest of the internet. Don't forget to change it if you did.
If I remember correctly (based on a question you asked earlier this year), you are running Docker for Windows. Take a look at the docs over here, it says
The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.
Hence, set pghostname to host.docker.internal instead of localhost.
Since you state, that you "need to connect to existing db in (your) computer", I will go ahead and assume you do this for development purposes, so this should suffice.
On production, however, you will need to run the database in a separate container and map the volumes.

Remote PostgreSQL connection with pgAdmin

I'm trying to set up a remote connection through PostgreSQL running on my server , based on Ubuntu 16.04. So far, when I click on the Save button on pgAdmin, it sort of freezes, does nothing. After typing .../manage.py runserver My_droplet_IP:5432, I try the webpage, and it is accessible.
I followed this tutorial after creating my droplet.
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04
Then I edited the settings.py; pg_hba.conf; postgresql.conf files
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresqlpsycopg2',
'NAME': '.....',
'USER': '....',
'PASSWORD': '....',
'HOST': '127.0.0.1',
'PORT': '5432',
STATICROOT = os.path.join(BASE_DIR, 'static/') - at the end of the page
And, ofcourse changed the ALLOWED HOSTS = ['....'] with my droplet ip aswell.
postgresql.conf listen_address is set to '*'
pg_hba.conf file:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
Also allowed firewall, and made an exception to 5432 to be allowed.
Any ideas?
First of all test if you can connect to the database via psql:
psql -h ip_address -d name_of_the_database -U username
If you get connection refused error you had to set up something wrong and check the What should I check if remote connect to PostgreSQL not working?
psql: could not connect to server: Connection refused Is the server running on host ip_address
What should I check if remote connect to PostgreSQL not working?
Check the authentication configuration in pg_hba.conf
Usually located on linux - /etc/postgresql/version/main/pg_hba.conf.
You should allow authentication for client for specific IP all from all IP addresses:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::0/0 md5
#all ips
host all all all md5
More information how to set up pg_hba.conf you can find in documentation.
Then you should set up listening on specific port.
You have to find the postgresql.conf. Usually located /etc/postgresql/9.1/main/postgresql.conf) file and change the line with listen_address from:
#listen_address = ''
to (don't forget remove # which means comment):
listen_address = '*'
After every step you should restart Postgresql service:
sudo service postgresql restart
After step 2 you should see port 5432 (or 5433) in listening address after netstat command:
netstat -ntlp
After that you have to open port for PostgreSQL in firewall:
sudo ufw allow 5432
You can check firewall settings with (you should see 5432 in the list):
sudo ufw status
If any of the previous step doesn't work you should check if PostgreSQL is not running on different port (usually 5433) and repeat the previous steps.
This happens very often when you have more running versions of PostgreSQL or you upgrade database and forgot stop the previous version of PostgreSQL.
If you have problems to find configuration files you can check this thread Where are my postgres *.conf files?.
In case you are using GCP remember to set the firewall rule inside GCP to allow that port, it might save you some hours of debugging.

while deplyoing django app on heroku postgres doesn't work

I am trying to deploy my django app on heroku server,i followed the instructions from this website https://devcenter.heroku.com/articles/getting-started-with-python#introduction .it worked fine till , "heroku open" command.When i came to the part where i need to host my database using " heroku run python manage.py syncdb" command , it failed showing the mesage "OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?". I tried lots of fixes including the one suggested here Deploying Django app's local postgres database to heroku? and http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html .I tried all the solutions including editing the "listen_address" = '*' and tcpip_socket='true' in postgresql.conf and editing the ipv4 and v6 values in pg_hba.conf to
host all all 127.0.0.1 255.255.0.1 trust
host all all 10.0.0.99/32 md5
host all all 0.0.0.0/0 .
But none of them worked .I am guessing the problem arises because heroku can not connect to my local postgres server.This is strange because i'm able to access the postgres server via pgadmin.
And also in the django settings.py looks like this
DATABASES =
{
'default':
{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_test',
'USER': 'postgres',
'PASSWORD': '******',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '5432',
}
}
Do i need to change this and use heroku's database settings instead??
localhost on the server points to the server not your local machine. The reason why is because the server running your django code will try and resolve the dns name localhost and it has a pointer to 127.0.0.1 which is local to the server resolving that name. That will NOT point to your computer you are working on.
You need to get an instance of postgres on heroku and change HOST: 'xxx.xxx.xxx.xxx to the IP address of your new postgres instance in your django settings.

Using Apache to serve Django project on RHEL (503 error)

I am a newbie to the whole website thing... Would really appreciate if you could give some help here...
What I want to do is host a Django project on a remote server (red hat, CentOS release 6.5)
I've been running test of the project on a remote server using the development server and port 8000:
python manage.py runserver *.*.*.*:8000 --insecure
In this case, the website works fine and accessible from other machines.
0 errors found
September 04, 2014 - 08:13:03
Django version 1.6.4, using settings 'mysite.settings'
Starting development server at http://*.*.*.*:8000/
Quit the server with CONTROL-C.
Now I want to put it in production, and I've chosen to use Apache http server and mod_wsgi. I have httpd and wsgi installed and activated. I changed the httpd.conf configuration file to:
Listen *:80 (I've also tried Listen *:8000 and Listen (IP address):8000)
#DocumentRoot "/var/www/html"
DocumentRoot "/testsite" (I put a plan html file under the directory just for test)
ServerName <here is the url of the site,with no port number>
However, when I try to open the webpage I am always having a 503 error:
Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime
or capacity problems. Please try again later.
Apache/2.2.3 (CentOS) Server at <site url> Port 80
I tried a couple of things (1) checked what's using the port 80:
~# sudo lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 28732 root 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28734 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28735 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28736 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28737 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28738 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28739 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28740 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
httpd 28741 apache 4u IPv6 19802111 0t0 TCP *:http (LISTEN)
~# service httpd status
httpd (pid 28732) is running...
(2) restart the apache server:
service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
(3) placed a plain .html in /var/www/html/testsite, the DocumentRoot directory for testing.
(4) I tried to run the django on a different port (such as 8008, 8001 and 80)
e.g. python manage.py runserver *.*.*.*:8008 --insecure
0 errors found
September 04, 2014 - 07:56:18
Django version 1.6.4, using settings 'mysite.settings'
Starting development server at http://*.*.*.*:8008/
Quit the server with CONTROL-C.
As shown above, in the terminal it looks like it's working , but I cannot even access the website from remote machines even using the development server. I tried different port numbers but only the port 8000 can be used. But why can I open the webpage on localhost when I change the port number? e.g. 127.0.0.1:8008 or 127.0.0.1:8080 will work.
I guess it can be the firewall setting, then I went to /etc/sysconfig/iptables, I found under the web section, there was only one line:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT
Then I added another line for testing:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8001 -j ACCEPT
Then tried the development again with port 8001. Again, it looks like it's woking on the remote server but not accessible from remote machines.
Sorry if I made this confusing and if I asked something really silly. Now, I have three questions that I really don't understand. First of all, the 503 error really annoys me. Even it shows the apache server is running (restart httpd is OK), nothing actually displays... Second of all, when using the development server why can I only use port 8000 but not any else? Finally, in the 503 error message, it shows apache runs on Port 80 even after I changed the Listen port to 8000 in the configuration file, why is this?
Thanks ahead for any help!
If that is your only configuration I don't see how Apache could be aware of your Django running 8000. There is no indication that you are making Apache to connect or proxy requests to running Django instance.
What you need to do is
Configure mod_wsgi for Apache
or
Configure fgci for Apache
You are free to choose any port with Django development server. You can configure the IP address and the port the development server listens to with command line parameters.
You can make the Django development server to listen all IP addresses, including public IP addresses on the server, as:
python manage.py runserver 0.0.0.0:8000
Also Apache logs can be read at /var/log/apache (or similar directory), so it should explain why you are getting 503.
I doubt iptables are not related to any way to your problem, but somehow Django development server is not listening to public IP address. You can easily try this by disabled iptables firewall on the server.