Similar a MySQL, PostgreSQL es muy famoso y tiene una base de datos de código abierto y gratuita.
Anteriormente hemos visto varias instalaciones, incluida la instalación del conjunto LAMP, la instalación de Apache2, la instalación de PHP5 y la instalación de MySQL.
En este artículo, veremos cómo instalar la base de datos postgreSQL en Linux desde el código fuente.
Paso 1: Descargar el código fuente de postgreSQL
Desde el sitio de descarga de postgreSQL, elije el sitio espejo que se encuentra en tu país.
1 |
# wget http://wwwmaster.postgresql.org/redir/198/f/source/v8.3.7/postgresql-8.3.7.tar.gz |
Paso 2: Instalar postgreSQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# tar xvfz postgresql-8.3.7.tar.gz # cd postgresql-8.3.7 # ./configure checking for sgmlspl... no configure: creating ./config.status config.status: creating GNUmakefile config.status: creating src/Makefile.global config.status: creating src/include/pg_config.h config.status: creating src/interfaces/ecpg/include/ecpg_config.h config.status: linking ./src/backend/port/tas/dummy.s to src/backend/port/tas.s config.status: linking ./src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c config.status: linking ./src/backend/port/sysv_sema.c to src/backend/port/pg_sema.c config.status: linking ./src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c config.status: linking ./src/backend/port/dynloader/linux.h to src/include/dynloader.h config.status: linking ./src/include/port/linux.h to src/include/pg_config_os.h config.status: linking ./src/makefiles/Makefile.linux to src/Makefile.port # make make[3]: Leaving directory `/usr/save/postgresql-8.3.7/contrib/spi' rm -rf ./testtablespace mkdir ./testtablespace make[2]: Leaving directory `/usr/save/postgresql-8.3.7/src/test/regress' make[1]: Leaving directory `/usr/save/postgresql-8.3.7/src' make -C config all make[1]: Entering directory `/usr/save/postgresql-8.3.7/config' make[1]: Nothing to be done for `all'. make[1]: Leaving directory `/usr/save/postgresql-8.3.7/config' All of PostgreSQL successfully made. Ready to install. # make install make -C test/regress install make[2]: Entering directory `/usr/save/postgresql-8.3.7/src/test/regress' /bin/sh ../../../config/install-sh -c pg_regress '/usr/local/pgsql/lib/pgxs/src/test/regress/pg_regress' make[2]: Leaving directory `/usr/save/postgresql-8.3.7/src/test/regress' make[1]: Leaving directory `/usr/save/postgresql-8.3.7/src' make -C config install make[1]: Entering directory `/usr/save/postgresql-8.3.7/config' mkdir -p -- /usr/local/pgsql/lib/pgxs/config /bin/sh ../config/install-sh -c -m 755 ./install-sh '/usr/local/pgsql/lib/pgxs/config/install-sh' /bin/sh ../config/install-sh -c -m 755 ./mkinstalldirs '/usr/local/pgsql/lib/pgxs/config/mkinstalldirs' make[1]: Leaving directory `/usr/save/postgresql-8.3.7/config' PostgreSQL installation complete. |
Opciones de PostgreSQL ./configure
Las siguientes son varias opciones que se pueden pasar a ./configure:
- –prefix = PREFIX instala archivos independientes de la arquitectura en PREFIX. La ubicación de instalación predeterminada es /usr/local/pgsql
- –enable-integer-datetimes. Habilita la compatibilidad de fecha/hora de enteros de 64 bits
- –enable-nls [= LANGUAGES]. habilita la compatibilidad con idiomas nativos
- –disable-shared. No construye bibliotecas compartidas
- –disable-rpath. No embebe la ruta de búsqueda de biblioteca compartida en ejecutables
- –disable-spinlocks. No usan spinlocks
- –enable-debug. Construir con símbolos de depuración (-g)
- –enable-profiling. Construir con perfiles habilitados
- –enable-dtrace. Construir con soporte DTrace
- –enable-depend. Activar el seguimiento automático de dependencias
- –enable-cassert. Habilita comprobaciones de aserción (para depuración)
- –enable-thread-safety. Hace que las bibliotecas del cliente sean seguras para subprocesos
- –enable-thread-safety-force. Forzar la seguridad del hilo a pesar del fallo de la prueba del hilo
- –disable-largefile. Omite el soporte para archivos grandes
- –with-docdir = DIR. Instala la documentación en DIR [PREFIX/doc]
- –without-docdir. No instala la documentación
- –with-includes=DIRS. Busca archivos de encabezado adicionales en DIRS
- –with- libraries=DIRS. Busca bibliotecas adicionales en DIRS
- –with-libs=DIRS. Escritura alternativa de –with-libraries
- –with-pgport=PORTNUM. Cambia el número de puerto predeterminado [5432]
- –with-tcl. Compila módulos Tcl (PL/Tcl)
- –with-tclconfig=DIR. tclConfig.sh está en DIR
- – with-perl. Compila módulos Perl (PL/Perl)
- – with-python. Compila módulos Python (PL/Python)
- – with-gssapi. Compila con soporte GSSAPI
- – with-krb5. Compila con soporte Kerberos 5
- –With-krb-srvnam=NAME. Nombre principal del servicio predeterminado en Kerberos [postgres]
- – with-pam. Compila con soporte PAM
- – with-ldap. Compila con soporte LDAP
- – with-bonjour. Compila con soporte Bonjour
- –with-openssl. Compila con soporte OpenSSL
- –without-readline. No usa GNU Readline ni BSD Libedit para editar
- –with-libedit- preferred. Utiliza BSD Libedit en lugar de GNU Readline
- –with-ossp-uuid. Usa la biblioteca UUS OSSP al compilar contrib/uuid-ossp
- –with-libxml. Compila con soporte XML
- –with-libxslt. Usa el soporte XSLT cuando compilas contrib/xml2
- –with-system-tzdata=DIR. Usa datos de zona horaria del sistema en DIR
- –with-zlib. No usa Zlib
- –with-gnu-ld. Asume que el compilador de C usa GNU ld [default = no]
Problema de instalación de PostgreSQL:
Puedes encontrarte el siguiente mensaje de error mientras realizas ./configure durante la instalación de postgreSQL.
1 2 3 4 5 6 7 |
# ./configure checking for -lreadline... no checking for -ledit... no configure: error: readline library not found If you have readline already installed, see config.log for details on the failure. It is possible the compiler isn't looking in the proper directory. Use --without-readline to disable readline support. |
Solución de instalación PostgreSQL:
Instala readline-devel y libtermcap-devel para resolver el problema anterior.
1 2 3 4 5 |
# rpm -ivh libtermcap-devel-2.0.8-46.1.i386.rpm readline-devel-5.1-1.1.i386.rpm warning: libtermcap-devel-2.0.8-46.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 1e5e0159 Preparing... ########################################### [100%] 1:libtermcap-devel ########################################### [ 50%] 2:readline-devel ########################################### [100%] |
Paso 3: Verificar la estructura del directorio postgreSQL
Después de la instalación, asegúrate de que los directorios bin, doc, include, lib, man y share se crean en el directorio predeterminado /usr/local/pgsql como se muestra a continuación.
1 2 3 4 5 6 7 8 |
# ls -l /usr/local/pgsql/ total 24 drwxr-xr-x 2 root root 4096 Apr 8 23:25 bin drwxr-xr-x 3 root root 4096 Apr 8 23:25 doc drwxr-xr-x 6 root root 4096 Apr 8 23:25 include drwxr-xr-x 3 root root 4096 Apr 8 23:25 lib drwxr-xr-x 4 root root 4096 Apr 8 23:25 man drwxr-xr-x 5 root root 4096 Apr 8 23:25 share |
Paso 4: Crear una cuenta de usuario postgreSQL
1 2 3 4 5 6 7 |
# adduser postgres # passwd postgres Changing password for user postgres. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. |
Paso 5: Crear un directorio de datos postgreSQL
Debes crear el directorio de datos de Postgres y hacer que Postgres sea el usuario como propietario.
1 2 3 4 5 6 |
# mkdir /usr/local/pgsql/data # chown postgres:postgres /usr/local/pgsql/data # ls -ld /usr/local/pgsql/data drwxr-xr-x 2 postgres postgres 4096 Apr 8 23:26 /usr/local/pgsql/data |
Paso 6: Inicializar el directorio de datos postgreSQL
Antes de que puedas comenzar a crear cualquier base de datos postgreSQL, el directorio de datos vacío creado en el paso anterior debe inicializarse usando el comando initdb como se muestra a continuación.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# su - postgres # /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/ The files belonging to this database system will be owned by user postgres This user must also own the server process. The database cluster will be initialized with locale en_US.UTF-8. The default database encoding has accordingly been set to UTF8. The default text search configuration will be set to "english". fixing permissions on existing directory /usr/local/pgsql/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers/max_fsm_pages ... 32MB/204800 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 conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok vacuuming database template1 ... ok copying template1 to template0 ... ok copying template1 to postgres ... ok WARNING: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the -A option the next time you run initdb. Success. You can now start the database server using: /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data or /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start |
Paso 7: Validar el directorio de datos postgreSQL
Asegúrate de que todos los archivos de configuración de Base de Datos de postgres (por ejemplo, postgresql.conf) se creen en el directorio de datos como se muestra a continuación.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ ls -l /usr/local/pgsql/data total 64 drwx------ 5 postgres postgres 4096 Apr 8 23:29 base drwx------ 2 postgres postgres 4096 Apr 8 23:29 global drwx------ 2 postgres postgres 4096 Apr 8 23:29 pg_clog -rw------- 1 postgres postgres 3429 Apr 8 23:29 pg_hba.conf -rw------- 1 postgres postgres 1460 Apr 8 23:29 pg_ident.conf drwx------ 4 postgres postgres 4096 Apr 8 23:29 pg_multixact drwx------ 2 postgres postgres 4096 Apr 8 23:29 pg_subtrans drwx------ 2 postgres postgres 4096 Apr 8 23:29 pg_tblspc drwx------ 2 postgres postgres 4096 Apr 8 23:29 pg_twophase -rw------- 1 postgres postgres 4 Apr 8 23:29 PG_VERSION drwx------ 3 postgres postgres 4096 Apr 8 23:29 pg_xlog -rw------- 1 postgres postgres 16592 Apr 8 23:29 postgresql.conf |
Paso 8: Iniciar la base de datos postgreSQL
Usa el comando postgres postmaster para iniciar el servidor postgreSQL en segundo plano como se muestra a continuación.
1 2 3 4 5 6 7 |
$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 & [1] 2222 $ cat logfile LOG: database system was shut down at 2009-04-08 23:29:50 PDT LOG: autovacuum launcher started LOG: database system is ready to accept connections |
Paso 9: Crear la base de datos postgreSQL y probar la instalación
Crea una base de datos de prueba y conéctate a ella para asegurarte de que la instalación se realizó correctamente, como se muestra a continuación. Una vez que comiences a usar la base de datos, debes realizar copias de seguridad con frecuencia, dado que es una práctica básica de seguridad.
1 2 3 4 5 6 7 8 9 10 11 12 |
$ /usr/local/pgsql/bin/createdb test $ /usr/local/pgsql/bin/psql test Welcome to psql 8.3.7, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit test=# |