postgresql

PostgreSQL => ALTER TABLE without timezone -> with timezone, using select for TZ

冷暖自知 提交于 2021-02-08 10:26:05
问题 I'm trying to convert the column, and preserve the data in the specified timezone. I have a script, but it errors out on the SELECT at the end. ALTER TABLE schema.table ALTER COLUMN column_date TYPE TIMESTAMP WITH TIME ZONE USING column_date AT TIME ZONE (SELECT value FROM schema.table WHERE id = 'timezone'); ERROR: cannot use subquery in transform expression I have time zones stored, but trying to pull that back to apply to the script is posing a challenge. I know I can simply hardcode the

PostgreSQL => ALTER TABLE without timezone -> with timezone, using select for TZ

旧巷老猫 提交于 2021-02-08 10:23:01
问题 I'm trying to convert the column, and preserve the data in the specified timezone. I have a script, but it errors out on the SELECT at the end. ALTER TABLE schema.table ALTER COLUMN column_date TYPE TIMESTAMP WITH TIME ZONE USING column_date AT TIME ZONE (SELECT value FROM schema.table WHERE id = 'timezone'); ERROR: cannot use subquery in transform expression I have time zones stored, but trying to pull that back to apply to the script is posing a challenge. I know I can simply hardcode the

How to use psycopg2 to retrieve a certain key's value from a postgres table which has key-value pairs

…衆ロ難τιáo~ 提交于 2021-02-08 10:01:59
问题 New to python, trying to use psycopg2 to read Postgres I am reading from a database table called deployment and trying to handle a Value from a table with three fields id, Key and Value import psycopg2 conn = psycopg2.connect(host="localhost",database=database, user=user, password=password) cur = conn.cursor() cur.execute("SELECT \"Value\" FROM deployment WHERE (\"Key\" = 'DUMPLOCATION')") records = cur.fetchall() print(json.dumps(records)) [["newdrive"]] I want this to be just "newdrive" so

How to insert geojson data to geometry field in postgresql

点点圈 提交于 2021-02-08 10:01:54
问题 I want to insert geoJSON to a geometry column of a table. I have already inserted CSV file to the same column following this tutorial, I wonder how to insert the geoJSON to any geometry column? I tried following this answer but could not get what is going on there. 回答1: Just use an update with the function ST_GeomFromGeoJSON: UPDATE mytable SET geom = ST_GeomFromGeoJSON(json_column); The following example inserts a GeoJSON point into a JSON column and afterwards updates the geometry column

Connecting PostgreSQL db to Django project using Docker

耗尽温柔 提交于 2021-02-08 09:51:55
问题 I have a problem connecting my Postgres database to django project on docker container. I have already tried every solution found on the internet and nothing seems to work. I've already added host all all all md5 to my pg_hba.conf file, listen_addresses = '*' to postgresql.conf. Here is log from lsof -i :5432: postgres 19774 postgres 5u IPv4 2046377 0t0 TCP *:postgresql (LISTEN) postgres 19774 postgres 6u IPv6 2046378 0t0 TCP *:postgresql (LISTEN) I've tried to open 5432 port by using ufw

Changing a primary key to a composite primary key

橙三吉。 提交于 2021-02-08 09:41:34
问题 I've been using a "normal" (non-composite) primary key for one of my tables. Now I want to change it to a composite primary key. My tables look something like this: -- Table 1 CREATE TABLE foo ( id SERIAL PRIMARY KEY, id2 INT, ... ) -- Table 2 CREATE TABLE bar ( id SERIAL PRIMARY KEY, id_foo INT REFERENCES foo (id) ) The problem here is that psql does not want to drop the old primary key, since other tables reference it. Is there any way of getting around this without dropping the whole

Migrating trigger from Oracle 11g to Postgresql 8.4

我是研究僧i 提交于 2021-02-08 09:27:11
问题 My trigger in Oracle looks like this… CREATE OR REPLACE TRIGGER example$example BEFORE UPDATE OR DELETE ON example FOR EACH ROW BEGIN INSERT INTO example$ VALUES ( :old.key, :old.name, :old.describe seq.nextVal ); END; I thought I could simply translate to Postgresql with this… CREATE OR REPLACE TRIGGER example$example BEFORE UPDATE OR DELETE ON example FOR EACH ROW BEGIN INSERT INTO example$ VALUES ( OLD.key, OLD.name, OLD.describe, NEXTVAL('seq') ); END; I'm getting an error at the end of

Setup SSH tunnel with Paramiko to access PostgreSQL

笑着哭i 提交于 2021-02-08 09:14:12
问题 I currently use Paramiko to access an SFTP server and connect to the PostgreSQL on the same server. I found many examples using sshtunnel to log on PostgreSQL. But I don't know how to do it with pure Paramiko. Currently my code looks something like: # establish SSH tunnel self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(hostname=host, username=user, password=password) # setup SFTP server self.sftp = self.ssh.open_sftp() # connect

How to connect to my Heroku PostgreSQL database from Java

為{幸葍}努か 提交于 2021-02-08 09:10:34
问题 I have a Heroku PostgreSQL database to which I can't connect from Java. I have tried different methods but without success. Below is my Java code: import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MainConfig { public static void main(String[] args) { try{ dataSource(); System.out.println("conected"); } catch(Exception ex){ ex.printStackTrace(); } } public static Connection dataSource() throws URISyntaxException, SQLException { String

How to connect to my Heroku PostgreSQL database from Java

允我心安 提交于 2021-02-08 09:07:20
问题 I have a Heroku PostgreSQL database to which I can't connect from Java. I have tried different methods but without success. Below is my Java code: import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MainConfig { public static void main(String[] args) { try{ dataSource(); System.out.println("conected"); } catch(Exception ex){ ex.printStackTrace(); } } public static Connection dataSource() throws URISyntaxException, SQLException { String