├── .gitignore └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | notes 3 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | UK Postcodes 2 | ============ 3 | 4 | A CSV of UK postal 5 | [out codes](http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Outward_code). 6 | 7 | - [MySQL Import Example](#mysql-import-example) 8 | - [PostgreSQL Import Example](#postgresql-import-example) 9 | - [MongoDB Import Example](#mongodb-import-example) 10 | 11 | ## Contributing 12 | 13 | Please feel free to add contributions and corrections to the CSV. Use a text 14 | editor as office applications and suites may add unnecessary formatting. 15 | 16 | ## MySQL Import Example 17 | 18 | Table creation. 19 | 20 | ~~~sql 21 | CREATE TABLE outcodes ( 22 | id INT NOT NULL AUTO_INCREMENT, 23 | postcode VARCHAR(5) NOT NULL, 24 | eastings INT(7) NOT NULL, 25 | northings INT(7) NOT NULL, 26 | latitude DECIMAL(10, 8) NOT NULL, 27 | longitude DECIMAL(11, 8) NOT NULL, 28 | town VARCHAR(255) NULL, 29 | region VARCHAR(255) NULL, 30 | country VARCHAR(3) NULL, 31 | country_string VARCHAR(255) NULL, 32 | PRIMARY KEY(id) 33 | ); 34 | ~~~ 35 | 36 | Import CSV file to table. 37 | 38 | ~~~bash 39 | mysql -u root -p --local-infile=1 40 | ~~~ 41 | 42 | ~~~sql 43 | LOAD DATA LOCAL INFILE '/path/to/postcodes.csv' 44 | INTO TABLE databasename.outcodes 45 | FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' 46 | IGNORE 1 LINES 47 | ( 48 | @postcode, @eastings, @northings, @latitude, @longitude, @town, @region, 49 | @country, @country_string 50 | ) 51 | SET 52 | postcode=@postcode, eastings=@eastings, northings=@northings, 53 | latitude=@latitude, longitude=@longitude, town=@town, region=@region, 54 | country=@country, country_string=@country_string 55 | ; 56 | ~~~ 57 | 58 | ## PostgreSQL Import Example 59 | 60 | Table creation. 61 | 62 | ~~~sql 63 | CREATE TABLE outcodes ( 64 | postcode VARCHAR(5) NOT NULL, 65 | eastings INT(7) NOT NULL, 66 | northings INT(7) NOT NULL, 67 | latitude DECIMAL(10, 8) NOT NULL, 68 | longitude DECIMAL(11, 8) NOT NULL, 69 | town VARCHAR(255) NULL, 70 | region VARCHAR(255) NULL, 71 | country VARCHAR(3) NULL, 72 | country_string VARCHAR(255) NULL, 73 | ); 74 | ~~~ 75 | 76 | 77 | Import CSV file to table. 78 | Navigate to the directory where your postcodes.csv is stored. 79 | 80 | ~~~bash 81 | psql [your_database_name] 82 | ~~~ 83 | 84 | ~~~sql 85 | \copy outcodes FROM 'postcodes.csv' WITH HEADER CSV 86 | ~~~ 87 | 88 | Add an additional 'id' column to make corrections and/or updates easier. 89 | 90 | ~~~sql 91 | ALTER TABLE outcodes ADD id serial NOT NULL PRIMARY KEY; 92 | ~~~ 93 | 94 | ## MongoDB Import Example 95 | 96 | ~~~bash 97 | mongoimport -d [your_database_name] -c [collection_name] --type csv --file postcodes.csv --headerline 98 | ~~~ 99 | --------------------------------------------------------------------------------