Please help, I have this stdin is not a tty message when i run the command below in my terminal.
psql -U postgres kdc < kdc.psql
kdc is the database and kdc.psql is the psql file with commands to populate the database. I am in the directory that holds the psql file.
I am not sure what causes that message (it does not happen here), but you should be able to avoid it using the -f option:
psql -U postgres -d kdc -f kdc.psql
Related
I have to get the names of Postgres databases in Python. How do I do that? I need to be able to put these names into a list.
you can use pg_dumpall and psql as superuser
pg_dumpall -h localhost -U postgres -f pg_dumpall.sql
I'm struggling getting data from an older postgres database to an newer one.
I have already tried a few things without success. For more information, see Useing ANSI driver to connect to a postgreSQL DB with python psycopg2
The last idea is now to use pg_dump and pg_restore.
from subprocess import PIPE, Popen
# destination handling has to be adjusted.
def dump_table(host_name, database_name, user_name, database_password, table_name):
command = 'pg_dump -h {0} -d {1} -U {2} -p 5432 -t public.{3} -Fc -f /tmp/table.dmp'.format(host_name, database_name, user_name, table_name)
p = Popen(command, shell=True, stdin=PIPE, universal_newlines=True)
return p.communicate('{}n'.format(database_password))
After some research, I came across the above approach. Unfortunately, I haven't really done anything with the shell yet. As I currently understand it, the postgresDB is addressed locally so that the table.dmp is stored there in the /tmp/ directory.
However, I do not have direct access to this directory in order to download files from it. Do I have a possibility to receive the file created here directly in Python in order to process it directly on the target server? Because the target server is the one I have access to.
I need to execute the following command from Python on Windows:
psql -h localhost -p 5432 -U postgres -f script.sql db_name
The above script works fine when ran from git bash / powershell. After entering the script in a terminal, I need to provide a password to confirm it (similar to when using sudo).
How can I do that? I keep finding solutions that I think are linux-based.
How do I do it on Windows? I have tried many variations of solutions involving subprocess, i.e:
import subprocess
p2 = subprocess.Popen(
'psql -h localhost -p 5432 -U postgres -f script.sql db_name',
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
print('this will print')
sudo_prompt = p2.communicate('THE_PASSWORD' + '\n')[1]
print('this will not')
A better option (more secure) than invoking psql with explicit mention of your password is to have a .pgpass file as described in the docs file (and keep it protected e.g. chmod 600 ~/.pgpass). This keeps your password out of the list of running processes.
On Windows:
On Microsoft Windows the file is named %APPDATA%\postgresql\pgpass.conf (where %APPDATA% refers to the Application Data subdirectory in the user's profile).
I want to execute a python script which executes the following command in order:
sudo su - postgres #change user to postgres
psql #enter the psql command promt from
create user user_name with password 'mypassword'; #
create database voylla_development with encoding = 'utf8'; #all the 3 commands are to be executed in psql command prompt
grant all on database voylla_development to user_name; #
exit #psql prompt
exit #postgres user
cat <backup_file_name> | zcat - | PGPASSWORD=mypassword psql -d voylla_development -h localhost -p 5432 -U user_name
I tried using subprocess and os.system():
cmd='sudo -u postgres psql'
args = shlex.split(cmd)
p=subprocess.Popen(args)
p.wait()
cmd1='psql'
args1 = shlex.split(cmd1)
p=subprocess.Popen(args1)
p.wait()
##and so on for each command
But the script stops after I login as postgres user. How can I continue the script after user change?
Thanks
EDIT: using psycopg2 helped the cause
When you create a new Popen() object, you start a new program. You are not communicating with an open psql shell.
You either have to drive psql directly by setting stdin to subprocess.PIPE, or, much easier, use pexpect to drive the psql shell:
import pexpect
psql = pexpect.spawn('psql')
psql.expect('=>') # wait for the prompt
psql.send('create user user_name with password 'mypassword';')
# etc.
I have written a code in Python which accesses Mysql database in my computer.My question is how do I make my program run on other machines i.e how do I transfer the database ??
Thank you for reading...
use the tools that come with MYSQL installation
from command line
backup
mysqldump -u root -p pass21 --databases yourdb > multibackup.sql
restore
mysql -u sadmin -p pass21 Customers < multibackup.sql
Backing-up-and-restoring-your-MySQL-Database