How to remote access PostgreSQL database and get its dump to local? - python

I need to access the remote database server(linux) and also take its dump to my local(mac).
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'test-name',
'USER': 'test-user',
'PASSWORD': 'pwd',
'HOST': 'test.amazonaws.com',
}
}

In local bash, you can connect and dump the remote database with pg_dump command, see Copying PostgreSQL database to another server
Using Python is similar, except that you need to use psycopg2 module to connect and send commands to database server. You may find this post useful Postgresql Database Backup Using Python
Alternatively, if you need to execute the script in the database server, a better idea would be dump it remotely, then fetch it to local machine.

Related

Trouble ussing Buddy to test a project using Django REST FRAMEWORK POSTGRE AS DB

Hello I'm currently trying to implement Buddy with my little StartUp that is using DJANGO REST FRAMEWORK as a base. I found a very good example on the site. Unfortunately in the exmaple the used a MySql DB and I'm using Postgre as DB. My settings.py is:
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'DBname',
'USER': 'DBuser',
'PASSWORD': 'DBpassword',
'HOST': '',
'PORT': '5432',
}
}
}
My Buddy execute look somethig like this:
pip install -r requirements.txt
cd Project
python manage.py test
I also created postgre service in Buddy
like version: latest, Hostname: postgres, Port:5432, Login:DBuser, Password:DBpassword, Name of a database to be created on container startup: DBname
When I run the build Test of my project an error message apears like this:
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
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.5432"?
Action failed: see logs above for details
I really don't now how to fix this, it appears that Buddy creates the database correctly but for some reason django does not reconise that it exists.
Pd: The proyect works fine on my laptop and my friends. I'ts only when I tried to run it on buddy where it gives me problems

Cannot connect to postgresql databse from django settings.py on localhost

Forgive me as I this is my first django project using postgresql (version 11.8). For now I just want to connect to a test database which I have set up locally using pgadmin4. When I create the database I am not given the option to add a password, but when I run python manage.py migrate it is insisting on a password. When I then set the password to "password" on pgadmin, django won't accept it. It's probably something really obvious as I am quite new to Django, but I have tried searching and not found the answer to my problem. In settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Last line of the error when I run python manage.py migrate:
django.db.utils.OperationalError: fe_sendauth: no password supplied
Any help much appreciated. Craig
When you installed PgAdmin4 it asked you to create a password. This is for the superuser postgres, which is what you are trying to connect as. Use psql to connect to the database using the above settings and supply the password and see if it works.
I resolved it by removing the Postgresql 11 server and reinstalling it.

Setting up Django to connect to local SQLExpress in windows

I have successfully installed and setup Django in windows. I have also created my first project using django default database setting.
However, I would like to change the DATABASE settings to connect to my local SQLExpress.
Here is my DATABASE settings in settings.py file:
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME': 'mytestdb',
'ENGINE': 'sqlserver_ado',
'HOST': 'localhost',
'USER': 'test_user',
'PASSWORD': '1234',
}
}
After I saved the file, I ran into a problem where it says "Could not open a connection to SQL Server [2]".
I am able to connect to my local SQLExpress server using SQL Server Management studio. I know that the server is running. I've tried to google it and tried different setups, and I couldn't get it running.
This is the error that I've got when I ran syncdb command:
python manage.py syncdb
django.db.utils.OperationalError: (com_error(-2147352567, 'Exception occurred.', (0, u'Microsoft SQL Server Native Clien
t 10.0', u'Named Pipes Provider: Could not open a connection to SQL Server [2]. ', None, 0, -2147467259), None), u'Error
opening connection: DATA SOURCE=localhost;Initial Catalog=mytestdb;UID=test_user;PWD=******;PROVIDER=sqlncli10;DataTy
peCompatibility=80;MARS Connection=True')
Here is my installed packages:
C:\windows\system32>pip freeze
Django==1.7.6
django-auth-ldap==1.2.5
django-mssql==1.6.2
pyodbc==3.0.7
python-ldap==2.4.19
pywin32==219
virtualenv==12.0.7
I am using Django development server.
Does the error "Could not open a connection to SQL Server [2]" is more to the user authentication error or couldn't find the SQL server?
In this case, your HOST string needs to contain both computer name or address and an Instance ID Las set during the installation procedure (SQLEXPRESS by default).
Use:
'HOST': 'COMPUTER-NAME\\SQLEXPRESS'
or
'HOST': '127.0.0.1\\SQLEXPRESS'

Setup and connect to database for Django on AWS

I've currently hosted a Django application on EC2 using Apache.
My database engine
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'subscribe',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
After setting up Apache, i can now access my web application on the public IP but I cannot perform any DB transcations as the tables don't exist. This is the error message:
ProgrammingError at /some-url
(1146, "Table 'subscribe.subscriberapp_subscriber' doesn't exist")
I know for sure this is because no migrations have been made post deploying to AWS. My question is, how do I setup the DB completely?
You need to run the initial migrations to create the tables. From the console verify that you have a connection to your db by running ./manage.py dbshell. If that works, you have the connection.
Then you need to either run the initial ./manage.py syncdb (for django <1.7) or if you're running django 1.7+ you will run ./manage.py migrate

Django on Google AppEngine with CloudSQL: How to connect database (Error 2002, Can't connect to local MySQL server..)

I'm trying to start a Django app to be hosted on GAE, using CloudSQL. Locally, I'm on Mac OSX Maverics, working within a virtualenv (via virtualenvwrapper).
After installing the GAE SDK for Python, I started my virtual environment, installed Django 1.5 from /usr/local/google_appengine/lib/django-1.5/
Also, on appengine.google.com I created a new app, and connected a CloudSQL instance to it (enable billing).
I'm able to create a new Django project, e.g. django-admin.py startproject test01, then I edit its settings.py to change the DATABASES definition per Google's instructions, e.g:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/myapp-test01:myapp-db-test01',
'NAME': 'test01',
'USER': 'test01',
}
}
I also added app.yaml to the root of the project folder, per Google's docs:
application: test01
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.5"
builtins:
- django_wsgi: on
This is where I hit roadblocks.
First: What exactly should be entered into the DATABASES for NAME and USER fields? The docs do not go into any detail.
Second, when I run: python manage.py syncdb to initialize the app, I get:
OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/desgn-test-01:db-test-01' (2)")
I do have MySQL installed, via brew install mysql (although I didn't do that inside the virtual environment), and I also have MySQL-python.
I'm new to GAE and fairly inexperienced with setting up databases, so I'm not sure what do try next. I'm not sure if the issue is with my local MySQL, or the CloudSQL connection settings?
(A more general Django on GAE question: What is the workflow exactly? If I get the connection to work, does it mean that I am using CloudSQL even when developing my Django app locally? How do I subsequently "push" the app to the AppEngine, or make updates? I'm assuming this is done with the Launcher but what is the correlation between creating (adding) an app using the appengine.google.com dashboard, versus adding a new app in the local launcher? Quite confused by this -- are these two one and the same app, need to have identical names, or..?)
Looks like at the time, you were missing the password field as well...
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test01',
'USER': 'test01',
'PASSWORD': '[password]',
'HOST': '/cloudsql/myapp-test01:myapp-db-test01',
'PORT': '3306',
}
Using the GUI for Cloud SQL, you can always add new Super Admin level users to the Cloud SQL instance in case you're unsure what user to use as well.

Categories