Showing posts with label programing. Show all posts
Showing posts with label programing. Show all posts

Postgres first steps on Ubuntu

Long story short: I was deploying one of my platforms on a virtual machine running Ubuntu 16.04.
As usual I start by installing Apache, Mod Python, my preferred editor (Geany), copy the Python files in the right place and here we reach a more complex step ... getting the database up.

Actually it is not complex but there are some things that you have to remember. If you don't do it in the correct order you might loose some time getting it right and this just happened to me yesterday so this morning I decided to write this post. Why? Because everywhere I look on the web the script is bloated with stuff that is not needed.

Here we are:

# Open terminal and install PostgreSQL as following:

sudo apt-get install postgresql libpq-dev postgresql-client postgresql-client-common

# Open pg_hba.conf  to see if everything is OK.
# Your current PostgreSQL version might be different (mine is 9.5 as you can see)
# please see yours and change the path bellow accordingly
sudo nano /etc/postgresql/9.5/main/pg_hba.conf

# You should see the following line immediately after the long comments at the beginning:
local   all             postgres                          peer

# if different please make it look with the example given, save the file (CTRL+O) and exit nano (CTRL+X)
# restart the service (not needed if you did not changed the file)
sudo service postgresql restart

# Enter PostgreSQL cursor
sudo -u postgres psql

# Now you are at the helm and can start doing the actual interesting stuff
# Get control over postgres user ... you will need it :)
ALTER USER postgres PASSWORD 'password1';


# Create your custom user
CREATE USER my_user;
ALTER USER my_user PASSWORD 'password2';

# This one depends on what you need to do with your user
ALTER USER my_user WITH SUPERUSER;

# Create the database
CREATE DATABASE my_database;
GRANT ALL privileges ON DATABASE my_database TO my_user; 


# Now you can exit postgres command line and add pgadmin3 to do the rest of the stuff using a GUI
sudo apt-get install pgadmin3

Enjoy :)

Not so much about publishing but ...

Asked about what industry I serve the answer is always "information industry". One of the aspects involved in this is programing, development of web applications and so on. In fact all of them are formatting, processing or distributing information.
I do a lot of my projects in Python and one thing that I've faced several times was the classic file upload.

Here it is the smallest snippet of Python code I use for that:

input type="file" name="selectedfile"
input type="submit" name="Submit" value="Upload"

upload_dir = "c:/"
fileitem = request.file('selectedfile')
name=fileitem.filename[string.rfind(fileitem.filename,'\\'):]
fout = file(str(upload_dir+name), 'wb')
while 1:
  chunk = fileitem.file.read(1000)
  if not chunk: break
fout.write (chunk)
fout.close()

I've looked over the web for something like that and all solutions looked like incomplete and not that direct.
Hope you will find it useful.