I wrote the following code to connect to the database and fill the schema:
db_url = 'postgresql+psycopg2:///bidder:<pass>#localhost:5432/basketball'
Engine = create_engine(db_url, echo=False)
SessionMaker = ORM.sessionmaker(bind=Engine, autoflush=False)
Session = ORM.scoped_session(SessionMaker)
Base.metadata.create_all(Engine)
That last statement, however, raises:
(psycopg2.OperationalError) FATAL: role "fran" does not exist
("fran" is my unix username)
Sqlalchemy is not connecting to the database with the username and password I'm specifying in db_url.
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+-------------+-------------+-----------------------
basketball | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | bidder=CTc/postgres
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
Users table:
rolname
----------
postgres
bidder
(2 rows)
Try removing one of the / from the db_url.
db_url = 'postgresql+psycopg2://bidder:<pass>#localhost:5432/basketball'
Related
I need to show all found databases name.
I have found solutions that obtain information from a known database but in my case I do not know the 'database_name.db'.
someone can help me please!
well, you don't have to :
psql -h localhost -U pguser -c '\l'
this way you will connect to default database (called postgres).
postgres connections need to connect to a database.
output:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
mytestdb | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 |
imdb | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 |
postgres | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 |
template0 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
I am trying to save hash_value in mysql database using python. I have obtained hash value hash = imagehash.dhash(Image.open('temp_face.jpg')) but after the execution of insert query cursor.execute("INSERT INTO image(hash,name,photo) VALUES(%d,%s,%s )", (hash,name, binary_image))it gives me error "Python 'imagehash' cannot be converted to a MySQL type".
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+-------------------+-------------------+
| hash | binary(32) | NO | PRI | NULL | |
| name | varchar(25) | NO | | NULL | |
| photo | blob | NO | | NULL | |
| arrival | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+---------+-------------+------+-----+-------------------+-------------------+
So what can be done to store the value or is there any other way to do the same task?
This seems to be the most obscure issue I have dealt with so far. I am struggling hard.
I have an application that collects remote data from some hardware and saves it to a database. I renamed one of the columns using alembic. I have a development database and a testing database. Both are on the same server (MySQL via MariaDB on CentOS 7).
I interact with the development server via the app running on my laptop and the testing db is interacted with via the app running on a clone of the production server.
All servers and databases are set up using Ansible, so differences are limited to usernames and passwords.
Below is the ansible snippet from the upgrade script
def upgrade():
op.alter_column('units', 'ip_address', new_column_name='ipv4', existing_type=sa.String(100))
If I run the app from my laptop (using my IDE), the data is saved ok.
If I run the script on the testing server manually (env)$ python app.py, the data is saved ok.
But, here's the problem, If I run the script using supervisord, I get an SQLAlchemy error. (excerpts below, full traceback)
...
File "thefile.py", line 64, in run
self.data['my_wellsites'][w.name]['ipv4'] = wellsite.unit.ipv4
...
sqlalchemy.exc.InternalError: (InternalError) (1054, u"Unknown column 'units.ipv4' in 'field list'") 'SELECT units.id AS units_id, units.unittype_id AS units_unittype_id, units.serial AS units_serial, units.ipv4 AS units_ipv4, units.mac_address AS units_mac_address, units.engine_hours AS units_engine_hours, units.gen_odometer AS units_gen_odometer, units.gen_periodic AS units_gen_periodic, units.notes AS units_notes \nFROM units \nWHERE units.id = %s' (1,)
...
SQLAlchemy models..
class Unit(Model):
__tablename__ = 'units'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, Sequence('unit_id_seq'), primary_key=True)
wellsites = relationship('Wellsite', order_by='Wellsite.id', backref='unit')
ipv4 = Column(String(16), unique=True)
...
class Wellsite(Model):
__tablename__ = 'wellsites'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, Sequence('wellsite_id_seq'), primary_key=True)
unit_id = Column(Integer, ForeignKey('units.id'), nullable=False)
...
etc/supervisord.conf..
...
[program:datacollect]
command = /home/webdev/mydevelopment/git/ers_data_app/env/bin/python /home/webdev/mydevelopment/git/ers_data_app/data_monitoring/collection_manager.py
stdout_logfile=/home/webdev/logs/datacollect.log
stderr_logfile=/home/webdev/logs/datacollecterr.log
autostart=true
autorestart=unexpected
startsecs=10
I tried running an additional alembic upgrade with
op.create_unique_constraint('uq_ipv4', 'units', ['ipv4'])
No dice.
The traceback is identical (using diff program) except timestamps
Here are the two database descriptions of the units table (identical)
MariaDB [ers_DEV]> show columns from units;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| unittype_id | int(11) | YES | MUL | NULL | |
| serial | varchar(10) | YES | UNI | NULL | |
| ipv4 | varchar(100) | YES | UNI | NULL | |
| mac_address | varchar(17) | YES | UNI | NULL | |
| engine_hours | int(11) | YES | | NULL | |
| gen_odometer | tinyint(1) | YES | | NULL | |
| gen_periodic | tinyint(1) | YES | | NULL | |
| notes | mediumtext | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
MariaDB [ers_TEST]> show columns from units;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| unittype_id | int(11) | YES | MUL | NULL | |
| serial | varchar(10) | YES | UNI | NULL | |
| ipv4 | varchar(100) | YES | UNI | NULL | |
| mac_address | varchar(17) | YES | UNI | NULL | |
| engine_hours | int(11) | YES | | NULL | |
| notes | mediumtext | YES | | NULL | |
| gen_odometer | tinyint(1) | YES | | NULL | |
| gen_periodic | tinyint(1) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
The issue was at the top of the /etc.supervisord.conf file. In it, an environment variable is set that was pointing the script to the wrong database, overriding the environment variable that was set and examined from anywhere else. This environment variable is only set when the script is being run from supervisor and was causing the trouble
I have a collation error with MySQL, specifically it is the following:
OperationalError: (1267, "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")
After checking, I noticed that it is related to certain emojis, mostly smileys.
I've checked my MySQL database and it is by default using utf8mb4 charset as shown:
+------------+---------------------------------------------------------------------------------------------------+
| Database | Create Database |
+------------+---------------------------------------------------------------------------------------------------+
| Dictionary | CREATE DATABASE `Dictionary` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ |
+------------+---------------------------------------------------------------------------------------------------+
My MySQL settings also indicate that everything needed is set to utf8mb4:
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
I am using Peewee (the Python module) to make my queries and the following line is the one that is causing the collation error
SingleWords.get(SingleWords.word == lowercase)
It is not much of a problem for me if I can't insert certain emojis, but I would still like to if possible. I have no idea why this is happening, any thoughts?
try this
SELECT * COLLATE latin1_general_ci FROM table;
this will select all with collate latin1_general_ci
I am trying to write a python script to automate the task of creating database using the most recent production dump. I am using psychopg2 for the purpose.
But after the creation of the new database I want to delete the previously used database. My idea is that if I could get the names of databases in a list and sort them, I can easily delete the unwanted database.
So, my question is : How can I get the names of the DBs in a list.
Thanks
You can list all of your DBs with
SELECT d.datname as "Name",
FROM pg_catalog.pg_database d
ORDER BY 1;
You can filter or order it whatever way you like.
psql -E -U postgres -c "\l"
The output of the above command is like
********* QUERY **********
SELECT d.datname as "Name",
pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
d.datcollate as "Collate",
d.datctype as "Ctype",
pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;
**************************
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+----------+----------+---------+-------+-----------------------
mickey | postgres | UTF8 | C | C |
mickeylite | postgres | UTF8 | C | C |
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)