Friday, January 24, 2020

PostgresSql option for creating table like another table


In MSSQL there is an easy, code-based, way to create a table with the same DDL and data as another table.
This is simply by running a query similar to this:



select * INTO "NEW TABLE" FROM "SOURCE TABLE"

I needed to do something similar in PostgreSQL and found this to be a nice way to recreate a table using another as a templet

CREATE TABLE TESTTABLE
AS
  SELECT *
  FROM "SOURCE TABLE"
  WHERE false;

psycopg2 on MacOS


I have recently been having some issues with install the python module psycopg2 on a mac os Mojave. No matter what modules I installed in the virtual environment, the pip install of psycopg2 would not find a suitable candidate.

I tried all sorts of ways to get around this, like installing the entire Postgresql engine via brew, all to no avail.

Eventually, I found this pip module (which I should have found earlier to be honest if I had been more attentive :) )

pip install psycopg2-binary