Tag Archives: postgresql

PostgreSQL: Drop a database which has a hyphen in name

Normally to drop a database in PostgreSQL, we can just type:

DROP DATABASE database;

Here’s a list of my current databases:

postgres=# \l
                                   List of databases
    Name     |   Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-------------+-----------+----------+-------------+-------------+-----------------------
 dashboard   | dashboard | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres    | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres-db | postgres  | UTF8     | en_US.UTF8  | en_US.UTF8  | 
 puppetdb    | puppetdb  | UTF8     | en_US.UTF8  | en_US.UTF8  | 
 template0   | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |           |          |             |             | postgres=CTc/postgres
 template1   | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |           |          |             |             | postgres=CTc/postgres
(6 rows)

And I want to drop a database called “postgres-db”. So, let’s try:

postgres=# DROP DATABASE postgres-db;
ERROR:  syntax error at or near "-"
LINE 1: drop database postgres-db;
                              ^

According to this, we have to escape the database name which contains a dash or hyphen in double quotation marks:

postgres=# DROP DATABASE "postgres-db";
DROP DATABASE

It works.

Install PostgreSQL server on Raspberry Pi from FreeBSD port

Before I wrote this blog post, I had thought there was no binary package for PostgreSQL server for FreeBSD 11 running on Raspberry Pi. Hmm… how wrong I was! I just wasted the whole night compiling Postgres from source. DOH!

Anyhow, here’s the step I took to do it. If you’re familiar with FreeBSD, there’s nothing new here. But, I only use FreeBSD once in awhile, and so I tend to forget a lot what I do.

cd /usr/ports/databases/postgresql94-server
make config
make install clean

After a very long time of waiting, I was presented with the following message:

To initialize the database, run                                                                                              
                                                                                                                             
  /usr/local/etc/rc.d/postgresql initdb                                                                                      
                                                                                                                             
You can then start PostgreSQL by running:                                                                                    
                                                                                                                             
  /usr/local/etc/rc.d/postgresql start                                                                                       
                                                                                                                             
For postmaster settings, see ~pgsql/data/postgresql.conf                                                                     
                                                                                                                             
NB. FreeBSD's PostgreSQL port logs to syslog by default                                                                      
    See ~pgsql/data/postgresql.conf for more info                                                                            
                                                                                                                             
======================================================================                                                       
                                                                                                                             
To run PostgreSQL at startup, add                                                                                            
'postgresql_enable="YES"' to /etc/rc.conf      

Let’s initialize the database:

 # /usr/local/etc/rc.d/postgresql initdb                                
Cannot 'initdb' postgresql. Set postgresql_enable to YES in /etc/rc.conf or use 'oneinitdb' instead of 'initdb'.

Ahh.. ok.

# echo 'postgresql_enable="YES"' >> /etc/rc.conf

Please note that I used ‘>>’ to append the above line to /etc/rc.conf. Alternatively, just append that line with a text editor such as Vim or Nano.

# /usr/local/etc/rc.d/postgresql initdb                                
The files belonging to this database system will be owned by user "pgsql".
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.

creating directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
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 
opying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

That’s it. Well, next time I want to install Postgres server again, I’ll run this command instead:

# pkg install postgresql94-server-9.4.2

Can’t find the ‘libpq-fe.h header

I need to install pg 0.17.1 gem on a server running CentOS. This is the error message I saw:

# gem install pg -v '0.17.1'
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...

First thing came to my mind was I must have forgotten to install the -dev package.

# yum install postgresql93-devel

Then, when I ran gem command again, suprisingly I still got the same error message. So I checked the explanation in the error message carefully.

# locate pg_config
/usr/pgsql-9.3/bin/pg_config
/usr/pgsql-9.3/share/man/man1/pg_config.1

# gem install pg -v '0.17.1' -- --with-pg-config=/usr/pgsql-9.3/bin/pg_config

This time, pg gem was successfully installed.

Note, if you use bundle to install gem, you’ll need to run this command:

# bundle config build.pg --with-pg-config=/usr/pgsql-9.3/bin/pg_config
# bundle install --path vendor/bundle

PostgreSQL: Peer authentication failed for user

Ever since Oracle took the ownership of MySQL, I’ve been moving toward PostgreSQL, or other NoSQL database like MongoDB.

I’m not completely new to PostgreSQL, but the fact that I only have to touch it once in awhile, makes me feel PostgreSQL is so hard to use and missing the goold-oldie MySQL.

Anyway, this is the issue I had:

# su - postgres
-bash-4.1$ psql -l
                                   List of databases
    Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------+----------+----------+-------------+-------------+-----------------------
 postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres-db | postgres | UTF8     | en_US.UTF8  | en_US.UTF8  |
 puppetdb    | puppetdb | UTF8     | en_US.UTF8  | en_US.UTF8  |
 template0   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
 template1   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
(5 rows)

-bash-4.1$ psql -d puppetdb -U puppetdb --password
Password for user puppetdb:
psql: FATAL:  Peer authentication failed for user "puppetdb"

To fix it, we need to modify: pg_hba.conf. This file is located in /var/lib/pgsql/9.3/data/ directory on my system.

Change it to:

local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

Ref: etting error: Peer authentication failed for user “postgres”,…