Code Monkey home page Code Monkey logo

access-to-postgresql's Introduction

access-to-PostgreSQL

This repository converts Access DB csv files to PostgreSQL insert scripts

General

This python codes, creates insert scripts from an existing MS Access database table for PostgreSQL.

The Process

To begin with, this codes do not directly get data from Access database. Initially the table in the Access DB should be exported as an MS Excel file.

Following these steps will make it very easy for me to export Access tables to PostgreSQL while doing a small scale project.

  1. Saving the relevant table from Access DB as an Excel table,
  2. Updating column names in Excel table ( if the column names defined in PostgreSQL are different from those in Access DB.)
  3. Editing the data (Date fields must be in PostgreSQL date format, NULL values, etc.)
  4. Saving the Excel file as a CSV file.
  5. Converting the saved CSV file to a data frame with the Python Pandas library, looping the row and column values of the data frame, writing the data as an insert script and saving the script as a line in a txt file,
  6. The scripts from the txt file and pasting them into the PGAdmin query window and running them .

Saving the relevant table as an Excel table from Access DB:

After selecting the relevant table, we select the "External Data" menu. Here we click on Excel. We save the Excel file by saying OK.

image

Updating column names in Excel table

We have to do this because the column names in PostgreSQL and in the Excel file have to be consistent!

image

Organizing data

Only the “ change_date ” column in the Excel table is in date format. We have to save this format as text. The PostgreSQL format is "YYYY-MM-DD". We can use Excel's translate-to-text function for this . The formula will be as follows. Of course, the excel I use is in Turkish. English users should write “YYYY-MM-DD”!

image

YYYY - > Year, 2026 MM-> Moon, 03 DD->Day,23 We convert the entire column to text with the fill handle. Then we paste it again in the “ change_date ” column. The Excel file will change as follows. Salaries are also in Excel currency format. We need to convert that into numbers as well.

image

Saving the Excel file as a CSV file.

Excel file as csv file in UTF-8 format

image

Converting the saved CSV file to a data frame with the Python Pandas library, looping the row and column values of the data frame, writing the data as an insert script and saving the script as a line in a txt file,

Let's create a new project in Anaconda Spyder . The appearance of the “100_salarychange.csv” file that we have saved in this project folder will be as follows. Here, decimals in numeric values are separated by “ , “ (comma). In this python, “ string ” will appear as a value. To avoid this, use the text editor to replace “,” with “.” The image is below:

image

The codes will convert the csv file to SQL script and save it to the file named sql.txt.

The appearance of the sql.txt file will be as follows.

image

Copy the scripts from the txt file and paste them into the PGAdmin query window and run them.

Just copy, paste and run!

image

access-to-postgresql's People

Contributors

mukiraz avatar

Stargazers

Sema Hatice DURGUT avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.