I am trying to create a table that logs my app in postgresql. Below works well.
conn = psycopg2.connect(database="db1", user = "postgres", password = "", host = "myhost", port = "5432")
cur = conn.cursor()
cur.execute('''CREATE TABLE login
(USERNAME VARCHAR(20) NOT NULL,
TS TIMESTAMP);''')
cur.execute("""INSERT INTO public.login (USERNAME,TS) \
VALUES ('TEST','2019-12-03')""");
my_table = pd.read_sql('SELECT * FROM public.login', conn)
conn.close()
I thought the table/relation would have been created and saved. But when I reconnect I get error that UndefinedTable: relation "public.login" does not exist.
conn = psycopg2.connect(database="db1", user = "postgres", password = "", host = "myhost", port = "5432")
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("""INSERT INTO public.login (USERNAME,TS) \
VALUES ('TEST','2019-12-03')""");
my_table = pd.read_sql('SELECT * FROM public.login', conn)
The table is not there with below, as answered in How do I get tables in postgres using psycopg2?
cur.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cur.fetchall():
print(table)
I am confused why closing connection would cause this.
You must call conn.commit()..
Related
I cannot query from other schema's than public, also I am using supabase and connection string provided in supabase doesn't work.
Use schema.table in the query.
conn_string = "dbname=postgres user=postgres password=*** host=*** port=****"
with psycopg.connect(conn_string) as conn:
with conn.cursor() as cur:
cur.execute('SELECT "userId" FROM next_auth.sessions ORDER BY expires DESC LIMIT 1')
result = cur.fetchone()
I am using Supabase and the port that they provided in their "connection string" in the admin panel was wrong.
I'm trying to connect to a postgres database by using psycopg2. I ask for the user and user password via input. But I think, this way doesnt let me connect to the database. Is there another way to place the values?
I try to connect like this:
connection = psycopg2.connect("host=localhost dbname=dbname user=%s password=%s", (username, password))
This is the error I get:
connection = _connect(dsn, connection_factory=connection_factory, **kwasync)
TypeError: 'tuple' object is not callable
There are a couple of ways to build the connection string from inputs, first:
# From input
user_name = 'some_name'
pwd = 'some_pwd'
connection = psycopg2.connect(host=localhost, dbname=dbname, user=user_name, password=pwd)
Second from here Make dsn:
from psycopg2.extensions import make_dsn
dsn = make_dsn('host=localhost dbname=dbname', user=user_name, password=pwd)
connection = psycopg2.connect(dsn)
UPDATE, forgot the most obvious way:
dsn = 'host=localhost dbname=dbname user=' + user_name + ' password=' + pwd
connection = psycopg2.connect(dsn)
I am trying to access a remote database of postgresql. I am working in a python project
I am able to access a table using psycopg2
import psycopg2
server_host = 'xx.x.xx.xx'
server_dbname = 'dbname'
server_username = 'username'
server_password = 'password'
connection_for_data_and_variables = psycopg2.connect(host=server_host, database=server_dbname, user=server_username, password=server_password)
cursor = connection_for_data_and_variables.cursor()
cursor.execute('SELECT r1, r2 FROM tablename WHERE t1=%s ORDER BY id DESC LIMIT 1;', ("Test", ))
a = cursor.fetchone()
I have the following in the /usr/share/webapps/phppgadmin/conf/config.inc.php of phpmyadmin
// Example for a second server (PostgreSQL for Windows)
$conf['servers'][1]['desc'] = 'Test Server';
$conf['servers'][1]['host'] = 'xx.x.xx.xx';
$conf['servers'][1]['port'] = 5432;
//$conf['servers'][1]['sslmode'] = '';
$conf['servers'][1]['defaultdb'] = 'template1';
$conf['servers'][1]['pg_dump_path'] = '';
$conf['servers'][1]['pg_dumpall_path'] = '';
When i try to login i get:
Also apache configuration:
/etc/httpd/conf/extra/phppgadmin.conf
Alias /phppgadmin "/usr/share/webapps/phppgadmin"
<Directory "/usr/share/webapps/phppgadmin">
DirectoryIndex index.php
AllowOverride All
Options FollowSymlinks
Require all granted
</Directory>
I have a psycopg2 connection which I am using to connect to postgresql from pyspark. Here is my code -
host = 'IP Address'
port = 'Port'
user = 'postgres'
db = 'postgres'
password = 'password'
def move_records(main_table,stg_table):
try:
connection = psycopg2.connect(host=host,
database=db,
user=user,
password=password,
#driver = driver,
port = port)
cursor = connection.cursor()
move_query = "INSERT INTO " +main_table+ " select * from "+stg_table+" where country ='USA'"
cursor.execute(move_query)
connection.commit()
logger.debug("Record moved successfully")
except (Exception, psycopg2.DatabaseError) as error :
logger.error("%s Error in transction Reverting all other operations of a transaction ", error)
global flag
flag = False
connection.rollback()
finally:
if(connection):
cursor.close()
connection.close()
logger.debug("PostgreSQL connection is closed")
move_records(table_1,table_2)
But I keep getting error below error on line if(connection):
"UnboundLocalError","evalue":"local variable 'connection' referenced before assignment"
Can not figure out what is the issue. Need help.
I am no expert in Python but I have worked on similar thing, connecting from Python to Postgres in in AWS Lambda using psycopg2.
I believe the error lies somewhere in scope of variable. You need to declare all variables(host, port, user, db, password) inside function once again as global or nonlocal and then try to run function.
For your reference, check out this link:-
I am experiment with poste.io mail server. It uses rouncube as its web interface. I tried to enable the password plugin.
I see below error whenever I try to set the password:
[21-Mar-2017 13:00:31 +0100]: DB Error: [1] no such function: update_passwd (SQL Query: SELECT update_passwd('$1$LXeDlIT0$NGunS8gcCOSrKK2ZJ6RIW/', 'naidu#example.com')) in /opt/www/webmail/program/lib/Roundcube/rcube_db.php on line 539 (POST /webmail/?_task=settings&_action=plugin.password-save)
The internet is full of using mysql as the database. I think I have to update the password change query in
/opt/www/webmail/plugins/password/config.inc.php
from
$sql = 'SELECT update_passwd(%c, %u)';
to
$sql = 'UPDATE mailaccount SET password=%c WHERE nname=%u LIMIT 1';
The UPDATE statement above is valid for mysql. What is the equivalent for sqlite3 database?
I have a setup consist of postfix with sqlite and my sql query is as below:
UPDATE mailbox SET password=%c WHERE username=%u LIMIT 1
My sqlite config as below:
$config['password_db_dsn'] = 'sqlite:////var/vmail/postfixadmin.db?mode=0646';
$config['password_query'] = 'UPDATE mailbox SET password=%c WHERE username=%u LIMIT 1';
Add this for debugging:
$config['debug_level'] = 4;
$config['sql_debug'] = true;
$config['imap_debug'] = true;
$config['ldap_debug'] = true;
$config['smtp_debug'] = true;
Hope this helps.