This isn't a Python scripting post, but the next one will be on the same topic. In this post I get a Postgresql database set up on my OpenBSD laptop and get familiar with the Postgresql environment.
I primarily use Microsoft SQL Server and vendor supplied database schemas at work. I know Postgresql has a good reputation among open source databases, but I haven't had an opportunity to use it in a work environment (I had a brief brush with Jigsaw years back - a competitor to Modular's MSSQL-based Powerview (Dispatch) in pit mining truck tracking database - but that doesn't count.)
Anyway, as I've noted in previous posts, I run OpenBSD as my operating system on my laptop at home. The OpenBSD project has a package for Postgresql.
The first order of business is to install the Postgresql server package. First, I'll set up a PKG_PATH FTP mirror location from within the ksh shell:
$ export PKG_PATH=ftp://ftp3.usa.openbsd.org/pub/OpenBSD/5.7/packages/i386/
That ftp3.usa.openbsd.org server is the one in Boulder, Colorado - that's the one I usually use. I'm in Tucson, Arizona in the Mountain timezone, so it kind of makes sense to use that one. My understanding is that, in general, you want to use a mirror away from the main one to spread out the bandwidth and server use for the OpenBSD (or any other open source) project.
Now to install the package - this has to be done as root. I use sudo for this (sudo's replacement, as I understand it, in OpenBSD 5.8 will be doas(1) although you'll still be able to get sudo(1) as a package).
$ echo $PKG_PATH
ftp://ftp3.usa.openbsd.org/pub/OpenBSD/5.7/packages/i386/
$ sudo pkg_add postgresql-server
quirks-2.54 signed on 2015-03-09T11:04:08Z
No change in quirks-2.54
postgresql-server-9.4.1p1 (extracting)
1%
1%
2%
3% ********
<etc.>
100%
postgresql-server-9.4.1p1 (installing)
0% useradd: Warning: home directory `/var/postgresql' doesn't exist, and -m was not specified
postgresql-server-9.4.1p1 (installing)|
1%
1%
2%
3% ********
<etc.>
100%
postgresql-server-9.4.1p1: ok
The following new rcscripts were installed: /etc/rc.d/postgresql
See rcctl(8) for details.
Look in /usr/local/share/doc/pkg-readmes for extra documentation.
$
Given an internet connection with decent speed, this all goes pretty quickly. The first set of per cent numbers are the download of the gzippped tar package binary, the second are the unzipping and install of the Postgresql binaries in the proper location in the operating system file hierarchy.
For years I had some trouble getting my head around setting up users for Postgresql and running the daemon. Much of my database experience is as an application user at work using Microsoft SQL Server. We use Windows Authentication there primarily. Working on my own UNIX-based (OpenBSD) home system is a little different.
Most of the problems I've had overcoming this user/security hump related to my lack of a good strong grasp of UNIX users and permissions (like I could do it in my sleep strong grasp). OpenBSD is a bit unique in that it has a special name for the postgresql unprivileged user: _postgresql. The underscore is a convention in OpenBSD for this general class of user, usually associated with a daemon that runs on startup or gets started by root, doesn't have a login (nor a password). Michael Lucas spends several pages with a good summary of the rational behind this, the history and its conventions in his authoritative OpenBSD book.
So, we want to take a look at the directory designated for Postgresql's data, /var/postgresql:
$ ls -lah /var | grep post
drwxr-xr-x 2 _postgresql _postgresql 512B May 19 17:52 postgresql
$ cd postgresql
There is no data directory there (just . and .. in the /var/postgresql directory - the 2 in the ls output). This is typically where I would get stuck in the past. I ended up doing it manually . . . and wrong, or at least in a way that was more difficult than necessary. Anyway, I recorded it that way, so I'll blog it as executed.
What I had difficulty understanding before was the whole unprivileged user concept. Basically you need to use su to log on as root, then further su to log on as _postgresql:
# THIS IS AN UNNECESSARY STEP - DON'T DO THIS
$ su
Password:
# su - _postgresql
$ mkdir /var/postgresql/data
$ ls -lah /var/postgresql
total 12
drwxr-xr-x 3 _postgresql _postgresql 512B Jun 4 19:06 .
drwxr-xr-x 23 root wheel 512B May 19 17:52 ..
drwxr-xr-x 2 _postgresql _postgresql 512B Jun 4 19:06 data
$ exit
# exit
$
# END UNNECESSARY STEP
Now I need a database cluster. I want to initialize it with support for UTF-8 because I have some text data with umlauts in it (non-ASCII):
$ su
Password:
# su - _postgresql
$ initdb -D /var/postgresql/data -U postgres -A md5 -E UTF8 -W
The files belonging to this database system will be owned by user "_postgresql".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 30
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /var/postgresql/data/base/1 ... ok
initializing pg_authid ... ok
Enter new superuser password:
Enter it again:
setting password ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... not supported on this platform
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
postgres -D /var/postgresql/data
or
pg_ctl -D /var/postgresql/data -l logfile start
$ exit
# exit
$ whoami
carl
$ pwd
/home/carl
A couple things:
1) There's a line in the output about fixing permissions on the existing data directory (this will show up as highlighted on the blog, possibly not in the planetpython blog feed) - had I done this correctly (just let initdb make the directory itself), that line would look something like this (I created another cluster while writing the blog just so I would understand how to do it right):
creating directory /var/postgresql/data4 ... ok
Right there in the initdb(1) man page: "Creating a database cluster consists of creating the directories in which the database date will live . . ." The man page goes on to explain how to get around permission problems, etc. in this process. Note to self: read the man page . . . carefully.
2) What I also learned is that you can make as many database clusters as you want, all with different data directories. postgres is the superuser name you see in the documentation and /var/postgresql/data is the directory, but, as demonstrated above in the output, you could put your data in a folder called data4. If you gave a different name at the -U switch in the initdb command, the superuser name would be different too. Or you could have more than one cluster with postgres named superusers but with different passwords.
All that said, one cluster per physical box and the conventional names are plenty for me - I'm just trying to get used to the Postgresql environment and get started.
At this point I need to start up the Postgresql daemon. In the package install above, the output mentions an rc script /etc/rc.d/postgresql. This is run by root - below is a demo of using it manually with su (instead of using it as part of an rc startup sequence at boot):
$ su
Password:
# /etc/rc.d/postgresql start
postgresql(ok)
# pgrep postgres
6960
10175
4748
29053
32758
26201
# /etc/rc.d/postgresql stop
postgresql(ok)
# pgrep postgres
All I did there was start the Postgresql daemon with the installed rc script, check to see that it's associated processes are running, then stop the daemon with the same script.
Me being me, I can't leave good enough alone. I want the control of starting and stopping the daemon when I decide to (I am running this on a laptop). As I understand it, pg_ctl is a wrapper program provided with the Postgresql install for even more low level commands and functionality. I use pg_ctl to run the daemon and start it with the _postgresql user account:
$ su
Password:
# su - _postgresql
$ pg_ctl -D /var/postgresql/data -l firstlog start
server starting
$ exit
# exit
$
I asked pg_ctl to make a specific log file for this session (firstlog - this will go in directory /var/postgresql/). The logs are human readable and I wanted to study them later to see what's going on (there's all kinds of stuff in there about autovacuum and what not - sorry, we're not covering that in this blog post - but I'll have it available later).
Shutting down (stopping) the daemon is pretty simple with pg_ctl - just a few more keystrokes than if I had done it from root with the rc script:
$ su
Password:
# su - _postgresql
$ pg_ctl -D /var/postgresql/data stop
waiting for server to shut down.... done
server stopped
$ exit
# exit
$ whoami
carl
$
Great - so I'm good for getting the daemon going when I want it and for designating my own specific log files per session. Now to create a user and get to work:
(with daemon running):
$ psql -U postgres
Password for user postgres:
psql (9.4.1)
Type "help" for help.
postgres=# CREATE ROLE carl SUPERUSER;
CREATE ROLE
postgres=# ALTER ROLE carl PASSWORD 'xxxxxxxx'
ALTER ROLE
postgres=# ALTER USER carl PASSWORD 'xxxxxxxx' LOGIN;
ALTER ROLE
postgres=# \q
$
I created a user/role carl with SUPERUSER capabilities within this instance of Postgresql. It's a bit ugly and I'm not sure I've done this correctly or the easiest way. Also, and of importance, I have given Postgresql user carl (not OpenBSD user carl) all permissions on everything. Really, carl only needs permissions to work on the database he's working on. Josh Drake (@linuxhiker on twitter) pointed this out to me. I am grateful for this. He is right. I am lazy.
Now to create my database. I got into model trains around Christmas of 2015 and went crazy collecting stuff and setting up a layout. I needed to somehow keep track of all the cars before it all got too unwieldy.
$ psql postgres carl
Password for user carl:
psql (9.4.1)
Type "help" for help.
postgres=# CREATE DATABASE hotrains;
CREATE DATABASE
postgres=# \q
$
The command line entry to start psql is something I'm a bit fuzzy on - postgres isn't, to the best of my knowledge, a database per se, but a means of connecting to psql when you don't want to designate a default database ot work on.
I'm not going to post the full database code for the sake of brevity - it's only 11 tables but that's a bit much for a blog post. Instead I'll post a graphic schema I made and talk to it a little bit before posting one related SQL code snippet.
Disclaimer: I'm not a designer. This schema diagram I did with Dia, a fairly lightweight Linux/UNIX desktop tool for flowcharts and stuff. I've never met a color palette or font choice I could simply let be. Asking me to do a flowchart with a lot of leeway on design is like leaving a two year old home alone with a Crayola 64 pack of crayons and the 300 year old family Bible - it can't end well.
All that said, I find schema diagrams helpful for showing relationships between tables and having an ugly one is better than none at all. I've embedded an svg version of it below; hopefully it shows up on the planetpython feed:
The focus of my crude toy database design was the use of foreign keys to maintain consistency in naming things I want to track: rail name for example. I went with "Santa Fe" where I could have went with (and probably should have) "ATSF." It doesn't matter as long as it's consistent and I know what it means.
Years ago I was called in to do some work on a blasting database at the mine. There weren't any constraints on the entry of names of blasting materials, but what could go wrong? There were only three or four products with four digit designators and "None." Well . . . it was a mess. I didn't want to take any chances on having a situation like that again, even, or especially, if I was doing all of the data entry. Foreign keys it was!
Here's a quick dump of the code I used to create the validsidenumbers table. The idea is to make sure the rail line or company name is consistent in all the side number records (yes, I did actually purchase some identical rolling stock with the exact same side numbers - it's a long story):
hotrains=# CREATE TABLE validsidenumbers (
railnamex varchar(50) REFERENCES validraillines (namex),
sidenumber varchar(50),
comments text,
PRIMARY KEY (railnamex, sidenumber)
);
CREATE TABLE
hotrains=#
That REFERENCES keyword sees to it that I won't enter anything typo'd or goofy into that railnamex column.
Next post is a Python one about pulling storing images of the train cars in the database and displaying them from within psql.
Thanks for stopping by.
Showing posts with label OpenBSD. Show all posts
Showing posts with label OpenBSD. Show all posts
Saturday, October 17, 2015
Sunday, May 17, 2015
Lenovo Thinkpad X201 Fan Replacement
This is not a Python-related post per se, but it may be useful to people getting started with UNIX-based, open source software, or even a Windows user who happens to be using a Thinkpad X201 laptop.
Background:
1) I use OpenBSD as my operating system because I am striving to learn UNIX and I find that distro the best for me for that purpose.
2) The venerable legacy IBM/current Lenovo Thinkpad line of laptops tends to be one of the best supported by OpenBSD and other BSD development communities (small, but loyal dev and user base).
3) I buy my Thinkpads refurb'd because they're cheaper that way.
4) Laptop parts only last so long before they start failing, more so with refurbished ones. It was easy to replace the hard drive; the fan is a bit more complicated in terms of disassembling the laptop.
5) I'm a bit mechanically challenged and tend to break things permanently when trying to fix them. This post hopefully will serve to help others overcome this lack of confidence and fear.
There is actually a really good step by step still frame photo series on the web about how to take apart a Thinkpad X201 and replace the fan. I used that extensively during this task:
http://www.myfixguide.com/manual/lenovo-thinkpad-x201-disassembly-clean-cooling-fan-remove-keyboard/
A walkthrough of my experience and a few notes:
Mr. Dexter's Star Wars joke about philips head screws (actually bolts) notwithstanding, stripping those little guys is a problem. I was lucky this time. In my model train adventures, I've been less so.
There are few things more annoying than a deep seated, little phillips head bolt or screw. The thought of taking a power drill to a laptop to extract one of these makes me a bit nervous. Fortunately, just before Radioshack went bankrupt a few months back, I found a nice set of long shaft phillips head screwdrivers in Tucson in one of their stores. Those tools have been indispensible.
This is the laptop after I got the fan hooked up. There is a forum on the internet from a few years back where someone is asking how to test the fan. People just kept trolling him and laughing at him. Here is how it's done. Basically you hook everything up (in my case I only needed the power, screen, and keyboard) without actually putting the laptop back together (those zillion phillips head bolts!) and boot up. It's hard to see, but the fan is happily whirring away over there on the left.
It's weird operating on a machine you're used to having in one piece - reminiscent of those scenes in STTNG where they take apart LCDR Data ("Data and Commander Riker are in engineering examining Data's head.")
Heating up:
You don't want to test the computer too long in this state (without the fan in the proper place and the machine put back together). Thinkpads and the X201 in particular are notorious for running hot. You can see from the screen that the machine is heating up at about a degree Centigrade in the time it takes me to type in the next sysctl command/query.
sudo shutdown -hp now
I put it all back together and the only (well, not really - see below) thing different was a slight nick on the keyboard:
Hey! Where did these "extra" screws (bolts) come from?! Uh-oh . . .
. . . and the sound doesn't work either - looks like I was a bit too hasty in putting this thing back together. We'll give it another try . . .
. . . it looks like some of those extra bolts hold that important piece of aluminum in place . . .
. . . and a few more over here . . .
. . . uh, OK, that's my problem with the sound :-(
That sound card connection is paper thin - I think that's why it has to be secured with a little snap-in thingy in the picture. Hardware is pretty amazing sometimes. To all my electrical engineering friends: I bow to you.
This time I test the laptop again, but for sound. Doing what computer techs do (taking apart laptops and fixing them)
I tell you folks
It's harder than it looks
(Sorry, had to).
After I got all the screws (bolts) put back in (save 4 - I have no idea and I'm leaving good enough alone), I still (thought) I had a problem with the sound. It turns out I've been through this before. On UNIX-based systems the X201 mute key works funkily. This link explains it a bit for a Debian Linux system. Whether OpenBSD is different under the hood or not, the behavior to the user is essentially the same:
http://www.stderr.nl/Blog/Hardware/Thinkpad/WeirdMuteButtonBehaviour.html
And I was good to go!
Hope this helps someone like me.
Thanks for stopping by.
Background:
1) I use OpenBSD as my operating system because I am striving to learn UNIX and I find that distro the best for me for that purpose.
2) The venerable legacy IBM/current Lenovo Thinkpad line of laptops tends to be one of the best supported by OpenBSD and other BSD development communities (small, but loyal dev and user base).
3) I buy my Thinkpads refurb'd because they're cheaper that way.
4) Laptop parts only last so long before they start failing, more so with refurbished ones. It was easy to replace the hard drive; the fan is a bit more complicated in terms of disassembling the laptop.
5) I'm a bit mechanically challenged and tend to break things permanently when trying to fix them. This post hopefully will serve to help others overcome this lack of confidence and fear.
There is actually a really good step by step still frame photo series on the web about how to take apart a Thinkpad X201 and replace the fan. I used that extensively during this task:
http://www.myfixguide.com/manual/lenovo-thinkpad-x201-disassembly-clean-cooling-fan-remove-keyboard/
A walkthrough of my experience and a few notes:
Mr. Dexter's Star Wars joke about philips head screws (actually bolts) notwithstanding, stripping those little guys is a problem. I was lucky this time. In my model train adventures, I've been less so.
There are few things more annoying than a deep seated, little phillips head bolt or screw. The thought of taking a power drill to a laptop to extract one of these makes me a bit nervous. Fortunately, just before Radioshack went bankrupt a few months back, I found a nice set of long shaft phillips head screwdrivers in Tucson in one of their stores. Those tools have been indispensible.
This is the laptop after I got the fan hooked up. There is a forum on the internet from a few years back where someone is asking how to test the fan. People just kept trolling him and laughing at him. Here is how it's done. Basically you hook everything up (in my case I only needed the power, screen, and keyboard) without actually putting the laptop back together (those zillion phillips head bolts!) and boot up. It's hard to see, but the fan is happily whirring away over there on the left.
It's weird operating on a machine you're used to having in one piece - reminiscent of those scenes in STTNG where they take apart LCDR Data ("Data and Commander Riker are in engineering examining Data's head.")
Heating up:
You don't want to test the computer too long in this state (without the fan in the proper place and the machine put back together). Thinkpads and the X201 in particular are notorious for running hot. You can see from the screen that the machine is heating up at about a degree Centigrade in the time it takes me to type in the next sysctl command/query.
sudo shutdown -hp now
I put it all back together and the only (well, not really - see below) thing different was a slight nick on the keyboard:
Hey! Where did these "extra" screws (bolts) come from?! Uh-oh . . .
. . . and the sound doesn't work either - looks like I was a bit too hasty in putting this thing back together. We'll give it another try . . .
. . . it looks like some of those extra bolts hold that important piece of aluminum in place . . .
. . . and a few more over here . . .
. . . uh, OK, that's my problem with the sound :-(
That sound card connection is paper thin - I think that's why it has to be secured with a little snap-in thingy in the picture. Hardware is pretty amazing sometimes. To all my electrical engineering friends: I bow to you.
This time I test the laptop again, but for sound. Doing what computer techs do (taking apart laptops and fixing them)
I tell you folks
It's harder than it looks
(Sorry, had to).
After I got all the screws (bolts) put back in (save 4 - I have no idea and I'm leaving good enough alone), I still (thought) I had a problem with the sound. It turns out I've been through this before. On UNIX-based systems the X201 mute key works funkily. This link explains it a bit for a Debian Linux system. Whether OpenBSD is different under the hood or not, the behavior to the user is essentially the same:
http://www.stderr.nl/Blog/Hardware/Thinkpad/WeirdMuteButtonBehaviour.html
And I was good to go!
Hope this helps someone like me.
Thanks for stopping by.
Subscribe to:
Posts (Atom)










