Postgres
First a small comparison with mysql/mariadb commands :
overview table :
| mysql | psql |
|---|---|
| show databases | l |
| use dbname; | \c dbname; |
| show tables; | \dt+; |
| describe tablename; | tablename |
| select version(); | select version(); |
| select now(); | select now(); |
| mysqldump | pg_dump |
configuration
This is mostly in /var/lib/pgsql/data
pg_hba.conf : PostgreSQL client authentication configuration file
authentication
By default postgres uses IDENT-based authentication and this will never allow you to login via -U (username) and -W (password) options. You need to login as that user and then connect.
| login as user | |
|---|---|
However you can disable it in the pg_hba.conf file by changing the appropriate line for user(s) from 'ident' to 'trust'
| pg_hba.conf | |
|---|---|
And of course a well-aimed
| restart | |
|---|---|
This method can also be used to give other host access besides localhost :
| trust hosts | |
|---|---|
And now, if you are on a standard centos 6.6 server the firewall will have the forward rules disabled. You could try to fix that and i will later on (TODO) but for now just disable the firewall since this is an internal test setup. service iptables stop Navicat and other hosts should now be able to reach postgresql.
restore database
In case of a crashes system, you can just run the raw files on the new system.
Important
This was only tested on an exact same version : 9.4 on both systems.
- Install postgres on the new system, and see if it works, you can also query where it keeps the data files:
| restore database | |
|---|---|
If this is a debian machine it probably will be there. My save was from another debian jessie, so the next actions where to copy the old files.
| copy old database | |
|---|---|
Check again and you will find the tables if all went well.
| result | |
|---|---|
find files
For instance the log and config file, they are indeed in ridiculous places. To find the config file will help since that will hint where the log file is as well. Login to psql as manager and run :
| find postgres files | |
|---|---|
Open it and look for the log file :
Of course it does not say where this pg_log directory is under, so :
An equally ridiculous location, but at least we have a log
| log file found | |
|---|---|
create as user
For test purposes in the backend i needed a database to be instantiated from scratch with stable id's to be able to test it with json queries.
This database has to created from scratch each time to get room for the correct ID's and most creation actions require admin privileges. The best (easiest) way to do it is to create a test user like this :
Now fill in the name and password and be sure to answer yes to :
| question | |
|---|---|
Now you still cannot log in because the database does not exist, and it will not work from within an sql script:
But you can drop and create these as use kloptest outside psql with the commands:
Now the easiest way to reinstall a complete test environment is to generate an .sql file with the pg_dump command. First fill the normal database as you please and then dump it with this command :
| dump db | |
|---|---|
Note that nowhere in this .sql file is the database name mentioned, and you can just re-import this as new in kloptest :
| reimport | |
|---|---|
I repeated it on purpose so you can see there are no errors about existing tables.
Important
If you redo this with a larger test database your tests will probably work because it starts numbering id's from 1 again. But if you are going to use names probably first find out how to seed 'faker' to get the same data as well.
troubleshooting
too many connections
The main problem here surfaced with a crash with this message :
Error
FATAL: remaining connection slots are reserved for non-replication superuser connections
It seems this is a matter of to many open connections. First let's see what up while still in gdb.
You can view all open connections with this query in psql :
Or a number :
| count open connections | |
|---|---|
This print 97 suggesting that the limit might be 100 ?! That is indeed what seems to be the limit. However if you do all database the count is 102
So it might be per database because otherwise why the extra 2 ?!
If we look further in the records, they almost all seem to be COMMIT's :
If we release the debug session, the count drops to 1 probably the psql session itself. So if we restart and try some actions from the website we indeed that every action rises the count with 1 or more connections.
If we look at the commands for filling orders and vehicles (happens upon each refresh) we see in orders_from_db_list() :
So the database simply goes out of scope, probably not the culprit? A test with printing Open and Close in the constructor/destructor also suggests that. It prints :
| construction and destruction | |
|---|---|
So trips and vehicles are prime suspects, and indeed in trips and vehicles there were still lines like this :
| instantiate | |
|---|---|
So these ALL have to be altered !!. In any file search for new and delete, remove them and recompile and you get a tour along all errors. This change will change the behavior of F5, which will now at least for refresh keep a count of 1 open connection. This now considered fixed.!! (monitor)
show database table counts
For the actual count :
| table counts | |
|---|---|
But who is going to remember that ?!
Here is what you do if you only want to see which tables are empty:
| print table data | |
|---|---|
It gives the next table, all tables with 8192 or less bytes are empty !! I do not know what the 8192 bytes contains, the 8192 bytes are probably used for keys, because simple tables with only INT fields are 0 bytes. Mostly you can expect a table with any rows in it to be 16KB or more !
select fail
Mostly because the tables do not belong to the user. Check with :
Just as an example, tables trips and vehicles would be 'select' able by klopt, the other would give an error.
If you want to see the privileges :
| dump users | |
|---|---|
As you see you don't need explicit privileges as long as you are owner !
ERROR: permission denied for database klopt
This message seems to say that your password was wrong but it is actually fairly clear.
Important
It is NOT the connection that fails but an operation !!
I encountered this when connecting to postgresql through libpqxx :
To debug this, try to connect by hand and also show the log messages :
So more lines than the program reports. And indeed if you run this query inside psql it will say the same.
The solution in this case was :
| grant privileges | |
|---|---|
schemas with @ at symbol
We use schema inside the postgres database for planner to separate users. However the unique handle we use for that is the email address. And postgres will complain if you just say :
| error | |
|---|---|
Also this will fail :
| error | |
|---|---|
The CLI of postgres will give you a hint if you use the TAB :
| use tab for completion | |
|---|---|
This will work.
Important
With weird characters, put the schema name in "quotes" and not the table.
psql empty id's
This bugged me hard because it looks like there are lines here without an id but in fact this is psql's way of displaying multiline data !!
| not empty ids but multiline cells | |
|---|---|
This line was created with the following command :
The +'s mean continued on the next line, and it is indeed 1 database row.